Question
1) Write a C program based on the output and given infor. Below: Part a) - define a char array to hold the input word.
1) Write a C program based on the output and given infor. Below:
Part a) - 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 tostrlen(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. Also, do not use any existing function from the string library and keep on reading until a word "quit" is read in, followed by any character. Hint: You can compare the word against the predefined terminating token quit by characters. Define a function int isQuit(char word[])which checks if word is "quit" and output is shown below: Insert a word and a character separated by blank: hi x
Input word is "hi". Contains 2 characters. Index of 'x' in it is -1
Insert a word and a character separated by blank: hi l
Input word is "hi". Contains 2 characters. Index of 'l' in it is 2
Insert a word and a character separated by blank: analog u
Input word is "analog". Contains 6 characters. Index of 'u' in it is 3
Insert a word and a character separated by blank: quit x
Part b) Write an ANSI-C program that takes input from Standard in, and outputs the occurrence count of digits 0-9 in the inputs.
- use getchar and putchar to read from the standard input. The program continues to read until EOF is entered. Then outputs the occurrence count of each digit in the input.
- use an integer array as the counters, dont use 10 individual counters.
- when a character is read in, how to find the corresponding counter? Dont use statement such as if (c=='0') else if (c=='1') else if (c=='2') else Instead, use the onestatement trick discussed in class, which takes advantage of the index of the character to figure out the corresponding counter in the array.
sample output: TTBY2042M DB131
^D (press Ctrl and D)
0: 1
1: 3
2: 2
3: 1
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started