Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

How to count number of palindromes within a string?

c palindrome string loop

  • Please log in to reply
1 reply to this topic

#1 erock618


    CC Lurker

  • New Member
  • Pip
  • 6 posts
  • Programming Language:C, Java
  • Learning:C

Posted 01 October 2012 - 02:27 AM

Hey all, I managed to run into some trouble again. I'm writing a program that prints out a strings length, word count, vowel count, etc. But I seem to be having a very hard time with counting the number of palindromes within a sentence. I know the concept of how one string has to be equal to another string, but for some reason I can't seem to put it into code in which it compiles correctly. I was wondering if I could receive some insight.

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], *s; //String that containts at most 256 as well as a pointer
int words = 1, count = 0,vowel_word = 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

    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

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
return 0;

How do I manage getting around to checking if any words are palindromes or not? And how can I implement this into
int is_palindrome(char my_sen[])
and still call upon it in main?
  • 0

#2 AKMafia001


    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 118 posts

Posted 02 October 2012 - 08:45 AM

Well! Have you tried anything yet?

It's pretty simple if you give it a try....

As you know that palindrome can be read the same from both sides, you can start comparing chars (letter) on both ends and so on incrementing from one end and decrementing on other. You can give it a try, if you get any problem you can ask for help here...

Another easy way to find out a palindrome is to make a copy of ur string, reverse it and then compare it....

You will need some built-in functions which are included in string header:
1. strcpy()
2. strrev()
3. strcmp()

Hope this helps!
  • 0
I think i'm able to write a code for printing "Hello, World!". Proud of that!

Also tagged with one or more of these keywords: c, palindrome, string, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download