1. Engineering
  2. Computer Science
  3. 41 specification write an ansic program that reads from standard...

Question: 41 specification write an ansic program that reads from standard...

Question details

4.1 Specification Write an ANSI-C program that reads from standard input a word (string with no spaces) followed by a character, and then outputs the word, the number of characters in the word, and the index of the character in the word. 4.2 Useful information Note that C has no string type, so when you declare literal hello, the internal representation is an array of characters, terminated by a special null character 10, which is added for you automatically. So char helloArrllhello will give helloArr a representation of h | e | I | I | o |八01 , which has size 6 bytes, not 5 As shown earlier, one way to read a word from the standard input is to use scanf, which is passed as argument a string variable, when using scanf (%s, arrayName) , the trailing character O is added for you 4.3 Implementation . name your program lab2C.c .define a char array to hold the input word. Assume each input word contains no more than 20 characters (so what is the minimum capacity the array should be decleared to have?). use scanf define a functionint length (char word[]) which returns the number of characters in word (excluding the trailing character \O). This function is similar to strlen (s) Clibrary function shown earlier, and s.length ) method in Java. (%s % c, ) to read the word and char. .

define a function int indexof (char wordI, char c) which returns the index (position of the first occurrence) of c in word. Return-1 if c does not occur in word. This function is similar to s.indexof ()method in Java . do not use any existing function from the string library. keep on reading until a word quit is read in, followed by any character. Hint: You can compare the word against the pre-defined terminating token quit by characters. Define a function int isQuit (char wordI]) which checks if word is quit. Later we will learn how to compare two strings using library functions). 4.4 Sample Inputs/Outputs: (output is on a single line) red 308 ⅝ gcc -Wall lab2C.c-o lab2c red 309 % lab2c Enter a word and a character separated by blank: hello x Input word is hello. Contains 5 characters. Index of x in it is -1 Enter a word and a character separated by blank: hello 1 Input word is hello. Contains 5 characters. Index of l in it is 2 Enter a word and a character separated by blank: beautifulWord u Input word is beautifulWord. Contains 13 characters. Index of u in it is3 Enter a word and a character separated by blank: quit x red 310 %

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution