Like Tree3Likes

palindrome

This is a discussion on palindrome within the C Programming forums, part of the General Programming Boards category; Can anyone please advise wad's wrong with this code...why do i still get 0 despite typing in radar... Code: 1 ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    39

    palindrome

    Can anyone please advise wad's wrong with this code...why do i still get 0 despite typing in radar...

    Code:
     1 #include <stdio.h>
      2 #include <string.h>
      3     
      4 int isPalindrome1(const char *str);
      5          
      6 int main ( ) 
      7 {
      8     char str [40];
      9     int result;
     10     scanf("%s",str);
     11 
     12     result=isPalindrome1(&str);
     13     printf("%d",result); 
     14 
     15     return 0;
     16 }
     17 
     18 int isPalindrome1 (const char *str)
     19 {
     20     char temp1 [40];
     21     char temp2 [40];
     22     int i=0;
     23     int length=0;
     24     int result=0;
     25 
     26     strcpy (temp1,str);
     27     length=strlen(temp1);
     28     for (i=0; i<length; i++)
     29         temp2[i]=temp1[length-i];
     30 
     31     if(strcmp(temp2,str)=='\0')
     32         result=1;
     33     else
     34         result=0;
     35 
     36     return result;
     37 }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    o...i got it
    Code:
      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 int isPalindrome1(const char *str); 
      5   
      6 int main ( )
      7 {
      8     char str [40];
      9     int result;
     10     scanf("%s",str);
     11 
     12     result=isPalindrome1(&str);
     13     printf("%d",result); 
     14 
     15     return 0;
     16 }
     17 
     18 int isPalindrome1 (const char *str)
     19 {
     20     char temp1 [40];
     21     char temp2 [40];
     22     int i=0;
     23     int length=0;
     24     int result=0;
     25 
     26    strcpy (temp1,str);
     27     length=strlen(temp1);
     28     for (i=0; i<=length; i++)
     29         temp2[i]=temp1[length-i-1];
     30     if(strcmp(temp2,str)=='\0')
     31         result=1;
     32     else
     33         result=0;
     34 
     35     return result;

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Very good, but there is a faster and easier way...

    Basically you compare the ends, moving in and exiting on the first difference...
    Code:
    int IsPalindrome(char *str)
      { int i = 0;
         int j = strlen(str) -1;
    
         while (j > i)
           { if ( str[i] != str[j])
               return 0;  // no it isn't
             i++;
             j--; }
        
         return 1; }      // yes it is!
    Last edited by CommonTater; 12-16-2011 at 08:44 PM.
    juice and jollykiddo like this.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    Thanks, m trying a anagram code oso...can u pls advise why this code isnt working....it's printing 0 even for anagrams....

    Code:
     1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 int isAnagram1(char *str1, char *str2);
      5 int isAnagram2(const char *str1, char *str2);
      6 int isAnagram3(const char *str1, const char *str2);
      7 void sort (char *str, int length);
      8 void swap (char *a, char*b);
      9 
     10 int main ( )
     11 {
     12     int result=0;
     13     //int check=0;
     14     //int triplecheck=0;
     15     char str1 [50];
     16     char str2 [50];
     17 
     18     scanf("%s %s", str1, str2);
     19 
     20     result=isAnagram1(&str1, &str2);
     21     //check=isAnagram2(&str1, &str2);
     22     //triplecheck=isAnagram3 (&str1, &str2);
     23 
     24     printf("%d\n", result);
     25     //printf("%d\n", check);
     26     //printf("%d\n", triplecheck);
     27 
     28     return 0;
     29 }
     30 
     31 int isAnagram1(char *str1, char *str2)
     32 {
     33     int length1=0;
     34     int length2=0;
     35     int result=0;
     36 
     37     length1=strlen(str1);
     38     length2=strlen(str2); 
     39     sort(&str1, length1); 
     40     sort(&str2, length2); 
     41      
     42     if (strcmp(str1,str2)=='\0')
     43         result=1; 
     44     else 
     45         result=0; 
     46  
     47     return result; 
     48 } 
     49  
     50 void sort (char *str, int length)
     51 { 
     52     int i=0;int j=0; 
     53      
     54     for (j=0; j<length; j++)
     55     for (i=j+1;i<length; i++)
     56         if (str[j]>str[i])
     57             swap( &str[i],&str[j]);
     58  } 
     59  
     60 void swap (char *a, char*b)
     61 { 
     62     char temp; 
     63     temp=a; 
     64     a=b; 
     65     b=temp;

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    there's a } on line 66

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    You could do us all a favour by not posting code with the line numbers included. The board adds it's own line numbers anyway - if you do it properly.

    Also, your swap function should be generating warnings - try fixing them.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    sorry...m not sure how to remove the line numbers...i'll take note...

    and i'm unsure how to fix the swap function....do i put a * in front of temp?

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    changed the swap function to this....but...now when i execute the programme...i get segmentation fault(core dumped)

    Code:
     void swap (char *a, char*b)
     {
        char temp;
        temp=*a;
        *a=*b; 
        *b=temp;
     }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    39 sort(&str1, length1);
    40 sort(&str2, length2);
    What about the warnings on these lines as well?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    hmmm....i know there are warnings on lines 20, 39 and 40 oso...but i reali dunno how to fix them....isnt the function sort expecting a string? so wads wrong when i send in the address?

    pls advise. tks a million

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    o...the programme runs fine when i remove both the & ....but why? i thot when i see * ...i must put & in the function call???

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Here's what to do.

    1. compile your code
    2. fix ALL the warnings and errors.
    3. if you don't understand a specific error message, then post your code and exact error messages.

    Because all this "well some stuff happens" and general guessing doesn't work.
    Adak likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    Hi...i wrote another version for anagrams...but i dun understand why does it always return 0...no warnings or errors in the function....can u help me to decipher the code the tell me what's wrong...thanks..

    Code:
     int isAnagram3(const char *str1, const char *str2)
     {
         char num = 'a';
         int i=0;
         int j=0;
         int count1=0;
         int count2=0;
         int length1=0;
         int length2=0;
         int result=1;
     
         length1=strlen(str1);
         length2=strlen(str2);
         for (num='a'; num<='z'; num++)
         {
             for (i=0;i<length1;i++)
                 if (str1[i]==num)
                     count1++;
             for (j=0; j<length2; j++)
                 if (str2[j]==num)
                     count2++;
     
             if (count1!=count2)
             {
                 result=0;break;
             }
         }
         return result;
     }

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    not sure why count1=1; count2=0....den they are not equals.....if they are anagrams, why is count1 !=count2?

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    added this on line 117....but no difference....count1=0;count2=0;

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the palindrome
    By haochao in forum C Programming
    Replies: 15
    Last Post: 10-11-2008, 11:29 PM
  2. the palindrome
    By haochao in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2008, 07:30 AM
  3. palindrome
    By nightingale in forum C Programming
    Replies: 2
    Last Post: 07-26-2003, 02:45 PM
  4. Palindrome
    By Ginny Morgan in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 04:04 PM
  5. Palindrome
    By a_learner in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 08:03 AM

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