CSC270 HW 10 Solution 2016

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 08:36, 26 April 2016 (EDT)


<showafterdate after="20160101 00:00" before="20160601 00:00">

/* hw10.c
   Kaitlyn Stumpf
   4/18/2016
   CSC 270

   This program prompts the user for a string
   and displays it back.
   
   1. it will read a string from the user (as shown above),
   2. the string will contain email addresses separated by spaces (one or more spaces between each address)
   3. it will print the name of each person, without the text following the @-sign, and
   4. it will skip the first character of each name. For example if the email is dthiebaut@smith.edu, it will print only thiebaut.
*/

#include <stdio.h>
#include <string.h>

int main() {
  char s[100];
  char * word;
  char * parsedWord;
  int containsPeriod = 0;

  //--- get a string of chars ---
  printf( "Enter a list of email addresses, separated by spaces: " );
  gets( s );
  printf("\n\n");
  
  // Line below used for testing
  // char s[] = "rdoc@disney.com lgrumpy@disney.com shappy@castle.com nsleepy@smith.edu zbashful@abc.com nsneezy@winter.org ddopey@cnn.com";
  
  // Use strtok to split string by space and @ sign.
  word = strtok (s," @");
  while (word != NULL) {
  	// Iterate through split strings 
  	// and print only the first half of email addresses
  	// by ignoring strings w a period.
  	
  	// Check for a period in the word.
  	for (int i = 0; i < strlen(word); i++) {
  		if (word[i] == 46) {
  			containsPeriod = 1;
  		}
  	}
  	
  	// If there is no period in the word:
  	if (containsPeriod == 0) {
  		// Get rid of first character in name.
  		parsedWord = word + 1;
  		// And print name.
    	printf ("%s\n", parsedWord);
  	}
  	word = strtok (NULL, " @");
  	containsPeriod = 0;
  }

  return 0;
}

</showafterdate>