Lab 5 : Search in Strings

Solution has been posted.

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/

In today's lab, you will modify the given program to search in strings using String functions. Especially the following functions

fscanf, fgetc, fgets, strlen, strchr, strstr, strcpy, strncpy, strcmp

Complete execution of the program should result in the following output. User input is marked in bold. 

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 in the input string.

Stage 1. 0.5 points:

--------

- Prompt the user for a string 

- store the string in a global character array

- display the length of the string

- You can assume the max length of input string (char array) is 81

Sample output:

Please enter a lower-case string (max length = 80):

  >all generalizations are all false

  Length of your input is: 33

Stage 2: (0.5 points):

-------

- Prompt the user for a character

- search for the character in the string (string entered by user in stage 1).

Sample output:

Please enter a character that you want to find in the input string:

  >z

  Character [z] is found.

Stage 3 : (1 point):

--------

- Prompt the user for a new word

- search the string (string entered by user in stage 1) for the word

Sample ouput:

Please enter a word that you want to find in the input string:

  >all

  Word [all] is found.

Stage 4. Extra credit ( 1 point):

-------------------------

  Use the word entered by the user in the stage 3 and check if the word is the first word in the input string(String entered in stage 1)

Sample Output:

Word [all] is the first word in the input string.

Hint: strstr() returns the starting position of the word as a character pointer. 

char *p = strstr(inputString, word);

p contains the starting position of "word" in "inputString"

From this, to get the actual starting position of word in inputString, just subtract inputString from p

 

int pos = p - inputString; // Remember inputString holds the reference to the array

If the word were in the first position, pos will be equal to 0

Note: If you are having trouble comparing two strings in stage 2 it could be because you are using 

fgets to read the user input. After entering the input if the user presses enter, fgets stores the newline also.

You will have to ensure that the last character is not a newline. You can do this by placing the following code after you do the fgets

inputString[strlen(inputString) - 1] = '\0';

Zyante reference:

The following Zante sections have materials related to string functions

https://zybooks.zyante.com/#/zybook/UICCS141Fall2015/chapter/6/section/15

https://zybooks.zyante.com/#/zybook/UICCS141Fall2015/chapter/6/section/16