Lab 7: Search in string
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 pay attention to these 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):
>all generalizations are all false
Length of your input is: 33
Please enter a character that you want to find in the input string:
>z
Character [z] is found.
Please enter a word that you want to find in the input string:
>all
Word [all] is found.
Word [all] is the first word [all] in the input string.
Stage 1 (1 point):
Ask user to input a lower-case string.
Create a char array (inputLine) to hold the input string. 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 its length.
Example:
Please enter a lower-case string (max length = 80):
>all generalizations are all false
Length of your input is: 33
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)
If the input character (for example 'z') is found, then output Character [z] is found.
If the input character (for example 'z') is not found, then output Character [z] is not found.
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)
If the input word (for example "all") is found, then output Word [all] is found.
If the input word (for example "all") is not found, then output Word [all] is not found.
Example:
Please enter a character that you want to find in the input string:
>z
Character [z] is found.
Please enter a word that you want to find in the input string:
>all
Word [all] is found.
Stage 3 (Extra Credit, 1 point):
Create another char array (firstWord).
Find the first word in the original input string inputLine (word-separator character is white-space (' ')).
Copy this first word from inputLine into array firstWord ( strcpy/strncpy)
Check whether or not the inputWord (read in Stage 2) is the same as the firstWord
If they are the same (for example, both input word and first word are "all"), then output Word [all] is the first word [all] in the input string.
If they are not the same (for example, input word is "are" and first word is "all"), then output Word [are] is not the first word [all] in the input string.
Example:
Word [all] is the first word [all] in the input string.
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 by appending '\0' at the end of input:
inputStirng[strlen(inputStirng) - 1] = '\0';
Reference links: http://stackoverflow.com/questions/3302255/c-scanf-vs-gets-vs-fgets
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 link: http://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c
Additional references can be found here:
http://logos.cs.uic.edu/Examples%20And%20Notes/misc/CStdLib.html#string.h
http://logos.cs.uic.edu/Examples%20And%20Notes/notes/C%2B%2B/Ch5Arrays/Strings.htm (but notice codes on this page are mostly in C++)
Submission:
You should work with a partner for a grade. Only one submission per group is necessary, but be sure to include your name if you work alone, or both people's names if you worked with a partner.
You should submit the lab to the Blackboard at the end of the lab session, by 50 minutes after the hour.