7 Replies - 3185 Views - Last Post: 02 October 2012 - 04:19 AM Rate Topic: -----

#1 erock618  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 12

How to count number of palindromes within a string?

Posted 01 October 2012 - 01:35 PM

Hey guys, I'm new here but I have a question that has been bothering me for a few days now. I am writing a program that counts the number of words, vowels, and palindromes within a string and I am stuck on the palindrome counting aspect of the program. So far the code I've written has yielded no result and I figured I would sign up and ask here. The purpose is to input code into the
int is_palindrome(char my_sen[])
function and then call upon it in my main method, but I need help on how to record a palindrome as I'm a beginner at C and programming itself. Here is my code so far.


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SENTENCE 256


int main(void){

char my_sen[SENTENCE],my_sen2[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 the 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[]){
    

}


Is This A Good Question/Topic? 0
  • +

Replies To: How to count number of palindromes within a string?

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 729
  • Joined: 27-June 09

Re: How to count number of palindromes within a string?

Posted 01 October 2012 - 02:22 PM

I suggest writing a function that determines if a word is a palindrome. You will then loop through each word of the sentence and plug it into the function. If the result is yes then increment a variable that represents the palindrome count. This leaves you with two things to figure out.

1. Write a function that tells whether a word is a palindrome or not.
2. Write a loop that extracts words from the sentence one at a time and plugs it into the function from step 1.
Was This Post Helpful? 0
  • +
  • -

#3 erock618  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 12

Re: How to count number of palindromes within a string?

Posted 01 October 2012 - 03:00 PM

I've tried so far using the reverse and copy functions between 2 strings(my original and a new one I just called my_sen) but so far all it's doing is giving me a result of zero.
Was This Post Helpful? 0
  • +
  • -

#4 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 729
  • Joined: 27-June 09

Re: How to count number of palindromes within a string?

Posted 01 October 2012 - 03:26 PM

Can you show us what you are working on or what you have tried? I'm not even sure if you are working on 1 or 2 from my suggestion if either of them.
Was This Post Helpful? 0
  • +
  • -

#5 erock618  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 12

Re: How to count number of palindromes within a string?

Posted 01 October 2012 - 04:06 PM

Well so far I've only made some variables as well as a for loop to go through the string and return it's string in reverse since I can't use strrev in Linux. Is that wrong to do?


int is_palindrome(char my_sen[]){
    int k, i , j;
	for (i = 0, j = strlen(my_sen)-1; i < j; i++, j--) {
		k = my_sen[i];
		my_sen[i] = my_sen[j];
		my_sen[j] = k;
	}
	return;
    
}


I then put this code into my main method, but I know that I have to count it out by words, I just don't know where to really go from here. So far it copies my other string(my_sen2) from the original and uses is_palindrome to reverse the string to determine whether or not the string is a palindrome, but this reverses the entire string, which is what I'm stuck on, plus I didn't even input a counter variable yet.

strcpy(my_sen2,my_sen);
is_palindrome(my_sen);
	
	if(strcmp(my_sen, my_sen2) == 0) {
		printf("The string is a Palindrome\n");
	} else {
		printf("The string is not a Palindrome\n");
	}

Was This Post Helpful? 0
  • +
  • -

#6 erock618  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-October 12

Re: How to count number of palindromes within a string?

Posted 01 October 2012 - 04:12 PM

Basically, I'm completely lost. I've never really encountered a function like this before and I don't know what's really required of it. I understand the concept, which is basically to go through the string word by word to determine whether or not it can be spelled backwards or not. If it can, it'd be stored as 1 to the counter variable and if not then it wouldn't be stored at all thus finally printing how many words are palindromes.
Was This Post Helpful? 0
  • +
  • -

#7 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 337
  • View blog
  • Posts: 729
  • Joined: 27-June 09

Re: How to count number of palindromes within a string?

Posted 01 October 2012 - 10:05 PM

I believe your logic for testing palindromes is sound, but you need a better way of copying individual words out of the original sentence. To make sure the palindrome logic works, you may want to just use two other strings that are one word long and see if it prints the expected sentence.

Words are defined as a group of characters that are in the alphabet. Here's some (pseudo?)code to get you started. Try to understand it and fill in the missing comments. Also, double check my syntax.

char cpysen[WORD];
char cpysenrev[WORD];
int j=0;
int palcount=0;

for(inti=0;i<SENTENCE;i++)
{
  while(isalpha(mysen[i]))
  {
     //copy mysen to other strings one character at a time (don't use strcpy).
     i++;
     j++;
  }
  //set "/0" in other strings
  //test for palindrome and increment counter accordingly
  j=0; //reset j for next word
}


Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: How to count number of palindromes within a string?

Posted 02 October 2012 - 04:19 AM

"Number of words that are palindomes." Darn, number of palindromes in a string would be far more interesting. e.g. I am Sami.

Right, your second look up is "Number of words in the sentence." Here, you just seem to be looking for space. Note, with such an approach you'll probably miss the last word.

Your vowel_count seems far more advanced that the rest of the code; I'm guessing you copied this from somewhere else? Bingo, first hit: http://stackoverflow...-least-3-vowels

From there, you start looking for other people to write the rest of the code? e.g. here and here.

Step one to completing your program; write your own damn code! You learn nothing by just taking an example, pasting it into your mess, and praying.

Look at what the code you've already taken is doing! The vowel_count actually parses words, if you were paying attention. You should be using that logic for the rest of it. Most of the assignment is based on words, not sentences.

I'm honestly loathe to put out any code at this point... Roughly, your want something like:
for each word in sentence:
	wordCount++
	totalWordLength += length of word
	if (vowel count word >2) vowelCount3++
	if (word is palindrome) palindromeCount++
print totals



Good luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1