Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4. Problem C Reading and manipulating character arrays 4.1 Specification Write an ANSI-C program that reads from standard input a word (string with no spaces)

4. Problem C Reading and manipulating character arrays 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 \0, which is added for you automatically. So char helloArr[]= "hello" will give helloArr a representation of 'h' 'e' 'l' 'l' 'o' '\0' , 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 '\0' 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 ("%s %c", ) to read the word and char. define a function int length(char word[]) which returns the number of characters in word (excluding the trailing character \0). This function is similar to strlen(s) C library function shown earlier, and s.length() method in Java. define a function int indexOf(char word[], 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 word[])which checks if word is "quit". Later we will learn how to compare two strings using library functions). such as strcmp). 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 l 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 is 3 Enter a word and a character separated by blank: quit x red 310 %

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions