search in string C program
in this lab, we want you to write a program using C string functions.
All function examples can be found in the following two pages:
http://www.cplusplus.com/reference/cstdio/ and http://www.cplusplus.com/reference/cstring/
Especially the following functions:
fgetc, fgets, strlen, strchr, strstr, strcpy, strncpy, strcmp
· The execution of the finished program should look what is shown below (User input is shown in bold).
Author: Hong Wang
Lab: Thursday 2pm
Program: Search in string
Please enter a lower-case string (max length = 80):
>123 5678 0
Length of your input is: 10
Please enter a character that you want to find in the input string:
>5
Character [5] is found.
Please enter a word that you want to find in the input string:
>567
Word [567] is found.
Word [567] is not the first word [123] in the input string.
· Stage 1 (1 point):
· Ask user to input a lower-case string.
· Create a char array to hold the input string (inputLine). You can assume the max length of input string (char array) is 81 ( i.e. #define MAX_INPUT_NUM 81)
· Get the input string (fgets).
· Count the length of input string (strlen), and output this length.
Example I/O:
Please enter a lower-case string (max length = 80):
>123 5678 0
Length of your input is: 10
· Stage 2 (1 point):
· Ask user to enter a character.
· Get the input character (fgetc).
· Check whether or not the character is found in input string (strchr), and output the check result.
· Ask user to enter a word (inputWord).
· Get the input word (fgets).
· Check whether or not the word is found in the input string (strstr), and output the check result.
Example I/O:
Please enter a character that you want to find in the input string:
>5
Character [5] is found.
Please enter a word that you want to find in the input string:
>567
Word [567] is found.
Example I/O:
Notes:
· In windows, when you press "enter" key, you input "\r\n", while in Linux/Unix only "\n" is input. You can use code like this to "end" your input string:
inputStirng[strlen(inputStirng) - 1] = '\0';
· When you read a character from stdin, the end-line characters may still be in buffer. You can use this code to clear input buffer:
while (getchar() != '\n');
Reference: http://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c