Hey guys,
This program does exactly what it is intended to do, I was just wondering if it is the best way to accomplish this task. The program searches 'sentence' for str1, str2, or str3 (as picked by the user), and displays whether or not it is found in the original sentence. I am learning C from a book and doing all of the challenges, like I said, this program works fine, just wondering if it is the BEST way doing this.
Thanks!
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
main(){
char *sentence="When the going gets tough, the tough stay put!";
char *str1="Going";
char *str2="tou";
char *str3="ay put";
int response=0;
printf("%s\n\n", sentence);
printf("1\t is \"%s\" in the sentence?\n", str1);
printf("2\t is \"%s\" in the sentence?\n", str2);
printf("3\t is \"%s\" in the sentence?\n", str3);
printf("4\t Quit\n\nPlease enter your choice...");
scanf("%d", &response);
switch(response){
case 1:
if (strstr(sentence,str1)!=NULL){
printf("Yes \"%s\" is found in %s\n", str1, sentence);
getch();
}
else
printf("Nope, the G is not capitalized in \"%s\"", sentence);
getch();
return;
case 2:
if (strstr(sentence, str2)!=NULL){
printf("Yes, \"%s\" is found in \"%s\"\n", str2, sentence);
getch();
}
else
printf("Nope!");
getch();
return;
case 3:
if (strstr(sentence, str3)!=NULL){
printf("Yes, \"%s\" is found in \"%s\"\n", str3, sentence);
getch();
}
else
printf("Nope!");
return;
case 4:
return;
}
}