#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    How to count number of palindromes in the sentence


    hello everyone;

    can anybody teach me how to make a program using c language that would count the number of palindromes in a sentences and display the palindromes disregarding special characters...

    example: Enter your sentence: " Ana and hannah together with her dad went to the beach.

    number of palindromes: 3

    ana
    hannah
    dad

    i really need help;

    its my first post in here, please forgive me if i commit mistakes

    i have here a sample snippets :

    int isPalindrome(char *word)
    {

    char *c, *d;


    c = d = word;


    while(*d)
    d++;
    d--;


    while(c < d) {
    if(*c != *d)
    return 0;
    c++; d--;
    }


    return 1;
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    I think it would be worth to describe your task more precisely. For example, do you want to count only palindromes consisting of a single whole word. For example sentence:

    "eju huje"

    Is the two-words palindrome. while

    "daddy"

    contains one palindrome which is shorter than the word.

    If you are going to solve a simple task - i.e. count whole-single-word palindromes, you need only to separate string into words and check whether each of them is a palindrome. I strongly recommend write two separate functions - one to find next word in a string, starting from given position and copying it into separate string - and the other to test whether word (which is passed in a string) is a palindrome.

    Also please note that you should use tags (code) ... (/code) to wrap your code in them and get the code pretty printed. Only use square brackets instead of round ones.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    thanks and more


    hi,

    i don't know whats the problem with this one
    if i input a one word :Example: dad

    it will print "palindrome"

    but if i input two or more words : Example : "eju huje"
    it will print :"not palindrome"


    #include<stdio.h>
    #include<conio.h> //just for the color or design
    #include<string.h>

    int main(){


    char a[100],b[100];
    int i,k;

    textcolor(YELLOW);
    textbackground(BLACK);


    clrscr();

    printf("Type the string: ");
    gets(a);

    k=strlen(a)-1;

    for(i=0; i<strlen(a); i++){
    b[i]=a[k];
    k--;
    }

    b[strlen(a)]='\0';

    k=strcmp(a,b);

    if (k==0){
    printf("palindrome");
    }
    else{

    printf("not palindrome");

    }

    getch();

    }
    


    please help me correct this code

    our activity exactly is
    1. to create a program that would test an input string if it is a palindrome or not, special characters will not be include but numbers does

    Example:
    Enter a string: " rac, a toyota car "
    //it will print "palindrome"


    2.create a program that would accept and test a string if it has a palindrome words

    Example: "my dad and mom went to Glenelg, Scotland"

    output:

    number of palindrome words: 3 dad mom Glenelg

    pardon if i don't know how to declare a function because we didn't tacle it yet


    thank you ;
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Please edit your post and put [code][/code] tags around your code.
    Copy again from your code editor so indentation is preserved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    
    int main(){
    
    
        char a[100],b[100];
        int i,k;
        
        textcolor(YELLOW);
        textbackground(BLACK);	
    
       
        clrscr();
    
        printf("Type the string: ");
        gets(a);
    
        k=strlen(a)-1;
    
        for(i=0; i<strlen(a); i++){
    	b[i]=a[k];
    	k--;
        }
    
        b[strlen(a)]='\0';
    
        k=strcmp(a,b);
    
        if (k==0){
    	printf("palindrome");
    	}
        else{
    
    	printf("not palindrome");
    
    	}
    
       
    
    
    
        
    
       
    
       getch();
    
    
    
    }
    
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > Enter a string: " rac, a toyota car "
    > //it will print "palindrome"
    Well the first thing you need to do is remove all the spaces and punctuation, so
    rac, a toyota car
    gets stored as
    racatoyotacar

    So that when you reverse it, the strcmp() will work as expected.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo