Thread: searching for palindrome word in a sentence

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

    searching for palindrome word in a sentence

    Hi,


    can you please help me in this task ,please

    I split the sentence into words but how can I pass every word to the function and test at

    Code:
    # include <stdlib.h>
    # include <stdio.h>
    # include <string.h>
    # include <ctype.h>
    # define SIZE 100
    
    int ispalindromes(char str[],int length);
    void split(char st[]);
    
    int main () {
      char st[SIZE],st1[SIZE],*token;
      
      int counter=0,i=0,leng;
      
      
      gets(st);
      
      token = strtok( st," ");
        while (token != NULL) {
            printf( "%s\n", token);
            token = strtok( NULL, " ");
    if(ispalindromes(token,strlen(token){
    counter++;
    }
                        
        }
       
     
                     
    
     
       
      
      
          system("pause");
        return 0;    
    }   
    
    
    int ispalindromes(char str[],int length){
        
        
       if ( length < 1 )
    {
    return 1; /* no more chars to compare, its a palindrome */
    }
    if ( str[0] == str[length-1] ) /* Are the two ends same? */
    {
    return ispalindromes( str+1, length-2 ); /* continue checking */
    }
    else
    {
    return 0; /* comparison failed, not a palindrome */
    } }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by c lover View Post
    Hi,


    can you please help me in this task ,please

    I split the sentence into words but how can I pass every word to the function and test at

    Code:
    # include <stdlib.h>
    # include <stdio.h>
    # include <string.h>
    # include <ctype.h>
    # define SIZE 100
    
    int ispalindromes(char str[],int length);
    void split(char st[]);
    
    int main () {
      char st[SIZE],st1[SIZE],*token;
      
      int counter=0,i=0,leng;
      
      
      gets(st);
      
      token = strtok( st," ");
        while (token != NULL) {
            printf( "%s\n", token);
            token = strtok( NULL, " ");
    if(ispalindromes(token,strlen(token){
    counter++;
    }
                        
        }
       
     
                     
    
     
       
      
      
          system("pause");
        return 0;    
    }   
    
    
    int ispalindromes(char str[],int length){
        
        
       if ( length < 1 )
    {
    return 1; /* no more chars to compare, its a palindrome */
    }
    if ( str[0] == str[length-1] ) /* Are the two ends same? */
    {
    return ispalindromes( str+1, length-2 ); /* continue checking */
    }
    else
    {
    return 0; /* comparison failed, not a palindrome */
    } }
    your 2nd strtok needs to be at the end of the loop ( move from line 19 to 25 )

    you also need to compile it and fix the syntax errors ( missing prens ')' )
    Last edited by sparkomemphis; 05-04-2012 at 12:51 PM. Reason: additional info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Frequency of a word in a Sentence
    By sunny` in forum C Programming
    Replies: 2
    Last Post: 04-18-2012, 09:39 AM
  2. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  3. sentence word breaks...
    By aker_y3k in forum C++ Programming
    Replies: 4
    Last Post: 10-13-2003, 11:52 AM
  4. Searching for words within a sentence
    By drdroid in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:09 AM
  5. Search Sentence for Word?
    By drdroid in forum C++ Programming
    Replies: 4
    Last Post: 02-20-2003, 01:15 PM