This program compares two strings and outputs however many times the letter is found in the word the user inputs. I'm having a problem with it getting a segmentation fault and can't figure out why? Am I somehow performing a buffer overflow with this code?
Code:#include <stdio.h> #include <string.h> int num_appearances(char word[], char letter); int main() { char wordstring[20]; char searchLetter; printf("Please enter the word you would like to scan:\n"); scanf("%s", &wordstring); printf("Please enter a letter to search for:\n"); scanf("%s", &searchLetter); int num_times = num_appearances(wordstring, searchLetter); if(num_times > 0){ printf("The letter %s appears %d times in the word %s\n", searchLetter, num_times, wordstring); } else printf("The letter %s appeared zero times in your word\n"); system("PAUSE"); return 0; } int num_appearances(char word[], char letter){ int i; int counter = 0; for(i=0; i != '\0'; i++){ if(strcmp(&word[i], &letter)==0){ counter++; } else continue; } return counter; }



1Likes
LinkBack URL
About LinkBacks


