Like Tree1Likes
  • 1 Post By Matticus

How to count the number of words that are palindromes within a string?

This is a discussion on How to count the number of words that are palindromes within a string? within the C Programming forums, part of the General Programming Boards category; Hey everyone, I'm an up and coming programmer who is just learning C and is pretty desperate for some help! ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    How to count the number of words that are palindromes within a string?

    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.*

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Imagine you have some text I gave you, and no computer.

    How would you find out if that short text had a palindrome in it, or not?

    Solve it by hand a few (or several times), and you'll start to notice the patterns in what you're doing, to find those palindromes (or find there are none). Those patterns can serve as the basis for your programs pseudo code, and then become actual code.

    But you have to know how to find palindromes, using simple patterns, first. Don't use grammar insights - your computer is a grammatical idiot! Keep it simple.

    It's pen and paper time! Think of a vise, closing in on the center, checking for palindromes as it goes.

    And welcome to the forum!

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    static int palindrome(const char *str)
    {
        const char *endp = str + strlen(str);
        int t = 0;
    
        while (str < endp && (t = *str++ == *--endp))
            ;
        return t;
    }
    
    static int read(char *to, size_t n)
    {
        int c;
    
        n--;
        while ((c = getchar()) != EOF && !isspace(c))
            if (n) {
                *to++ = c;
                n--;
            }
        *to = '\0';
        return c;
    }
    
    static unsigned int countvowels(const char *str)
    {
        unsigned int n = 0;
    
        while (*str)
            n += !!strchr("aeiouAEIOU", *str++);
        return n;
    }
    
    int main(void)
    {
        unsigned int npalin = 0, nwords = 0, nvowels = 0;
        char word[1024];
        int c;
    
        while ((c = read(word, sizeof word)) != EOF) {
            if (word[0]) {
                npalin += palindrome(word);
                nvowels += countvowels(word);
                nwords++;
            }
            if (c == '\n') {
                printf("%u %u %u\n", npalin, nwords, nvowels);
                npalin = nwords = nvowels = 0;
            }
        }
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    @Barney McGrew - Please don't hand out full solutions. We are here to assist people with problems on their journey of learning, not to simply give them answers to copy + paste.
    Click_here likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-02-2011, 08:19 PM
  2. what would I use to count number of words
    By s_jsstevens in forum C Programming
    Replies: 42
    Last Post: 08-11-2009, 07:14 AM
  3. what would I use to count number of words
    By yuuh in forum C Programming
    Replies: 1
    Last Post: 08-11-2009, 07:10 AM
  4. Count number of uppercase words in array
    By stevedawg85 in forum C++ Programming
    Replies: 12
    Last Post: 04-10-2006, 12:38 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21