IPC144 Lab 10 - More Strings

Weekly Tip: A character within a string is an array element. We can change any string character or swap it with another character just as we change or swap int or double array element values.

In this lab, you will work with strings again. If you have problems, ask for help - that is what the lab sessions are for.

To begin with, enter the following program and call it lab10a.c

   #include <stdio.h>
   int nWords(char str[]);
   int main( ) {
      char string[301];
      printf("Enter a string: ");
      scanf("%300[^\n]", string);
      printf("The number of words in this string is %d\n", nWords(string));
   }
   int nWords(char str[ ]) {
      int i = 0, aword = 0, nwords = 0;
      while(str[i]) {
         if (str[i] == ' ' || str[i] == '\t' || str[i] == '\n') {
            if (aword) {
               nwords++;
               aword = 0;
            }
         }
         else
            aword = 1;
         i++;
      }
      if (aword)
         nwords++;
      return nwords;
   }
  1. This program counts the number of words in a string. Use the program first as a walkthrough exercise, to determine the output given a blank string and a non-blank one. Run your program to check the results of your walkthrough. Run some other examples to complete your test.

    Notice the following points:



  2. Copy lab10a.c to lab10b.c. In lab10b.c, convert the "while" loop to an appropriate "for" loop.

 

  1. Enter the program shown below and call it lab10c.c.  Then write the function int HasEmbedded(char str[]).  This function is to return 1 if there is an space embedded in letters in str and 0 if there isn't. The string that is passed should not be altered in any way.  These are examples of strings that do have embedded spaces: "Cookie Monster", "Kermit the Frog", "   Miss Piggy   ".  These are examples of strings that do NOT have embedded spaces: "Hobbes", "   Muppet", "  Gummi   ", "BooBoo   ", "  a1A*  ".
   #include <stdio.h>
   int HasEmbedded(char str[]);
   int main( ) {
      char string[31];
      int space;
      printf("Enter a string: ");
      scanf("%30[^\n]", string);
      space = HasEmbedded(string);
      if (space > 0)
	 printf("%s does have embedded spaces\n", string);
      else
	 printf("%s does NOT have embedded spaces\n", string);
   }

 

  1. Enter the program shown below and call it lab10d.c.  Then write the function void Cap(char str[]) that when given a string str will capitalize any character that is a lower case alphabetic character. Note that the string could contain any number of characters, including numbers, funny symbols, etc. 
	#include <stdio.h>
	void Cap(char str[]);
	int main( ) {
		char string[31];
		printf("Enter a string of 30 characters or less: ");
		scanf("%30[^\n]", string);
		printf("Capitalized: %s\n", Cap(string));
	}
 

Submission Requirements:

For Murray Saul's sections, please follow the submission commands (or character strings to type) followed by <ENTER> :


script
cc lab10b.c –o lab10b


lab10b
"Kermit the Frog"
lab10b
"  BooBoo   "
lab10b
"  a1A*$  "

mv typescript lab10.lst

mail -s "144lab10" -c $USER@learn.senecac.on.ca   murray.saul@senecac.on.ca < lab10.lst