Code:
/* Eric
4-12-2012
CIT-145
Program 4
This program will give the user 4 options. In the 1st, the user will enter two words.
The program will determine if the words are identical and tell the user if they are or if they are not.
Option 2 will prompt the user to enter a sentence. The program will count the number of words
and display the sentence and word count to the user. Option 3 will allow the user to enter 2 strings.
The program will display each individual string, and display a new string combining them.
Option 4 will close the program.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//Prototype functions
int compareStrings(char*, char*); //Option 1 - Compare strings
int countWords(char text); //Option 2- count words
char* combineString(char string1, char string2); //Option 3- combine strings
// Function main executes the program
int main( void )
{
int choice = 0; //Variable to hold users selection
char first[20];
char second[20];
char result;
char text[20];
int counter = 0;
char string1[20];
char string2[20];
while( choice !=4 ){ //loop for selection
printf("Please enter 1, 2, 3, or 4 for the option you would like to select: \n"
"1- Compare two words to see if they are identical. \n2- Count the number of words in a sentence. \n"
"3- Enter two strings and have them combined. \n4- End the program.\n");
scanf("%d", &choice);
switch( choice ) { //sends the user choice to each function
case 1:
//retieve words from user
printf("Enter first string: ");
scanf("%s", &first);
printf("Enter second string: ");
scanf("%s", &second);
//send to function compareStrings
result = compareStrings(first, second);
//display results
if ( result == 0 )
printf("The words are identical.\n\n");
else
printf("The words are not identical.\n\n");
break;
case 2:
//Get sentence
printf( "Please enter a sentence: " );
scanf(" %[^\n]", text);
//Display sentence
printf(" \nYour sentence was: %s\n", text);
//Count & display number of words
printf( "The total number of words is %d\n\n", countWords(text), counter );
break;
case 3:
printf("Please enter first string: ");
scanf(" %[^\n]", string1);
printf("Please enter second string: ");
scanf(" %[^\n]", string2);
printf("\nFirst string is: %s\n", string1);
printf("Second string is: %s\n", string2);
printf("Combined string is: %s\n\n", combineString(string1,string2));
break;
case 4:
//ends program
break;
default:
printf("Please make a valid selection.\n\n");
break;
}//End switch
}//End while loop
}// Ends function main
//Function compareStrings compares strings for option 1
int compareStrings(char *first, char *second)
{
while(*first==*second) //compare characters of each string
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}//End function compareStrings
//Function countWords counts the words for Option 2
int countWords(char text[])
{
int counter = 1; //initialize counter
int i = 0; //temporary array counter
while ( text[i] != '\0' ) { //loop to check for spaces
if( isspace(text[i]) ){
++counter;
}
i++;
}
return counter;
}//End function countWords
//Function combineString for option 3
char* combineString(char string1, char string2)
{
char stringCombined[42];
strcat(stringCombined, string1);
strcat(stringCombined, " ");
strcat(stringCombined, string2);
return stringCombined;
}//End function combineString