Well, I am at it again. I am making another menu-driven system with a bunch of functions that does things to strings. I am set on the first two options, but I am having trouble with option 3. You will see it as the function occurrence. Here is the code. The problem specifically is that it wont let me input the string in the function.
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char str[250];
/*Determines if the input string is a palindrome*/
void palindrome(){
char rev[250];
strcpy(rev,str);
strrev(rev);
if (*str==*rev){
printf("\nYes. The string is a palindrome\n\n");}
else{
printf("\nNo. The string is not a palindrom\n\n");}
}
/*Determines the number of words in the input string*/
void number_words(){
int words = 1;
char *w;
for (w=str; *w != '\0'; w++) {
if (*w==' ')
words++;
}
printf("\nThe number of words is %d\n\n", words);
}
/*With another string, it determines how many times the new string word exists in the original input string*/
int occurrences(){
int *p,*s,*b, occur;
char occ[250];
printf("Enter a word:");
gets(occ);
printf("\n");
for (s=str; *s != '\0'; s++){
b = strstr(s, occ);
if (*b != 250){
occur++;
b = strstr(b+1, occ);
if (*b != 250){
occur++;
}
else
break;
}
else
break;
}
printf("The number of times occurred is %d\n\n", occur);
}
/*With yet another string, it determines if this input string exists a whole in the original input string*/
void substring(){
}
/*displays the menu, select choice*/
void menu() {
int a;
printf("1. Check if the string is a palindrome\n"
"2. Number of words in a string\n"
"3. Number of occurrences of input word\n"
"4. Substring test\n"
"5. Exit\n"
"Enter an option: ");
scanf ("%d", &a);
switch (a)
{
case 1: palindrome(); break;
case 2: number_words(); break;
case 3: occurrences(); break;
case 4: substring(); break;
case 5: exit(0);;
}
}
/*prints the menu and applies the entire menu through an infinite loop until exit is selected*/
int main()
{
printf("Enter a string:"); /*gets the string from the user*/
gets(str);
fflush (stdin);
printf("\n");
while(1) {
menu();
}
system("PAUSE");
return 0;
}