Hey everyone, I'm an up and coming programmer who is just learning C and is pretty desperate for some help! I am writing a program that counts the number of words, vowels, and palindromes within an inputted sentence and I seem to be at the end of my rope while trying to figure out the palindrome aspect of it. I was wondering if any of you guys(or girls) could help me figure out how to count the number of palindromes that are within my string.
Here is my code so far(Nothing is on palindromes as nothing has worked so far.) and everytime I seem to write a loop to record palindromes, my output always seems to be 1 or 0. Any thoughts?
Code:#include <stdio.h> #include <string.h> #include <ctype.h> #define SENTENCE 256 int main(void){ char my_sen[SENTENCE], *s; //String that containts at most 256 as well as a pointer int words = 1, count = 0; //Integer variables being defined int i,vowel = 0, length; //More definitions printf("Enter a sentence: ");//Input sentence gets(my_sen);//Receives and processes input length = strlen(my_sen); //Stores the length of the input within length for(i=0;my_sen[i] != '\0'; i++){ if(my_sen[i]=='a' || my_sen[i]=='e' || my_sen[i]=='i' || my_sen[i]=='o' || my_sen[i]=='u' || //Loop that states if the input contains any of the following my_sen[i]=='A' || my_sen[i]=='E' || my_sen[i]=='I' || my_sen[i]=='O' || my_sen[i]=='U') //characters(in this case, vowels), then it shall be { //stored to be later printed vowel++; } if(my_sen[i]==' ' || my_sen[i]=='!' || my_sen[i]=='.' || my_sen[i]==',' || my_sen[i]==';' || //Similar to the vowel loop, but this time my_sen[i]=='?') //if the following characters are scanned within the input { //then the length of the characters within the input is length--; //subtracted } } for(s = my_sen; *s != '\0'; s++){ //Loop that stores the number of words typed after if(*s == ' '){ //each following space count++; } } printf("The sentence entered is %u characters long.\n", length); //Simply prints the number of characters within the input printf("Number of words in the sentence: %d\n", count + 1); // Adding 1 to t[he count to keep track of the last word printf("Average length of a word in the input: %d\n", length/count);//Prints the average length of words in the input printf("Total Number of Vowels: %d\n", vowel);//Prints the number of vowels in the input printf("Average number of vowels: %d\n", vowel/count);//Prints the average number of vowels within the input printf("Number of words that contain at least 3 vowels: %d\n",vowel_count(my_sen));//Prints number of words that contain at least 3 vowels printf("Number of words that are palindomes: %d\n", is_palindrome(my_sen)); return 0; } int vowel_count(char my_sen[]) { int wcount = 0; // number of words with 3+ vowel chars int vcount = 0; // current number of vowel chars in the current word int i = 0; // index into the string int ch; while ((ch = my_sen[i++]) != '\0') { if (isspace(ch) || !isalpha(ch)) { // ch is not an alphabetical char, which can happen either // before a word or after a word. // If it's after a word, the running vowel count can be >= 3 // and we need to count this word in. wcount += vcount >= 3; // add 1 to wcount if vcount >= 3 vcount = 0; // reset the running vowel counter continue; // skip spaces and non-alphabetical chars } if (strchr("aeiouAEIOU", ch) != NULL) // if ch is one of these { ++vcount; // count vowels } } // If my_sen[] ends with an alphabetical char, // which belongs to the last word, we haven't yet // had a chance to process its vcount. We only // do that in the above code when seeing a non- // alphabetical char following a word, but the // loop body doesn't execute for the final ch='\0'. wcount += vcount >= 3; // add 1 to wcount if vcount >= 3 return wcount; } int is_palindrome(char my_sen[]){ }
*By the way, I need to use is_palindrome to contain the loop and then call upon it later within my print statement.*



1Likes
LinkBack URL
About LinkBacks


