Thread: Pailindrome Function and Reverse Fucntion Problem

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    11

    Pailindrome Function and Reverse Fucntion Problem

    Actually I was supposed to make a program to check for a entered string that weather its a palindrome or not, I wanted to make a function isPal() but it all jinxed and im all confused where to start and how to work on it, Actually I know that this program is not a difficult task if I'd have used string.h header file, but I want to take inputs character by character, and output as string ... I thought that it would be easy but it blew my head, but now I'm more stuck on it to only do it this way or find a solution of this way character-by-character.

    It seems like my approach to this problem is kinda wrong, please correct me, or suggest me any idea soon ...

    For the mean time, here's my incomplete effort:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #define LIM 	100
    #define TRUE 	1
    
    
    void StRev(char array[],char array2[])
    {
       int c,x;
       while(array[c]!='\0')
    	 ++c;
    
       	for(x=0;x<c;x++)
       	{
         	array2[x]=array[c-x];
     	  }
    }
    
    int isPal(char arr2[])
    {
       char ch[LIM];
       StRev(ch);
    
       if(ch==arr2)
         return 1;
       else
         return 0;
    
    
    }
    int main()
    {
        char In[LIM],out[LIM];
        int count=0;
        printf("Enter a String: ");
        while( In[count] != '\x0D' );
        {
           In[count++]=getche();
         }
    
    
         if(isPal(In)==1)
           printf("yes");
         else
           printf("No");
     return 0;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    The equality operator (==) is not defined for arrays of characters. Use strcmp() to compare strings.

    Also, you can use strlen() to get the length of a null-terminated string.

    It looks like you ought to get to know the <string.h> library. Here's some documentation for it: http://www.opengroup.org/onlinepubs/.../string.h.html
    Last edited by joshdick; 05-09-2005 at 10:52 AM.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    11
    Quote Originally Posted by joshdick
    The equality operator (==) is not defined for arrays of characters. Use strcmp() to compare strings.

    Also, you can use strlen() to get the length of a null-terminated string.

    It looks like you ought to get to know the <string.h> library.

    Actually I wanted to do it manually, without using stings.h header file ... I have said this above ... and its also probably on bold ....

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Sorry I missed that. In that case, I suggest just implementing your own versions of those functions.

    For the function to reverse a string, your logic is wrong. You need to swap the characters. First, the first and last characters; then, the second and second to last characters; etc.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    11
    Quote Originally Posted by joshdick
    Sorry I missed that. In that case, I suggest just implementing your own versions of those functions.

    For the function to reverse a string, your logic is wrong. You need to swap the characters. First, the first and last characters; then, the second and second to last characters; etc.

    Here's the code but its still not working ...

    Code:
    #include<stdio.h>
    #include<conio.h>
    #define LIM 	100
    #define TRUE 	1
    
    void swapCh(char var1,char var2)
    {
       char temp;
          temp=var1;
          var1=var2;
          var2=temp;
    }
    
    void StRev(char array[],char array2[])
    {
      char  ch;
      int   count=0;
      int   x,y;
    
        while(array[count] != '\0')
    	 ++count;
    
        for(x=count,y=0;x<count;x--,y++)
    	swapCh(array[y],array2[x]);
    
    
    }
    
    int isPal(char arr2[])
    {
       char ch[LIM];
       StRev(arr2,ch);
    
       if(ch==arr2)
         return 1;
       else
         return 0;
    
    
    }
    int main()
    {
        char In[LIM],out[LIM];
        int count=0;
        clrscr();
        printf("Enter a String: ");
        while( In[count++] != 13 )
        {
           In[count]=getche();
         }
         StRev(In,out);
         printf("\n%s",out);
    
         if(isPal(In)==1)
           printf("\nyes");
         else
           printf("\nNo");
     return 0;
    }

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you cant compare strings that way you need to use strcmp() to compare strings. look it up in your helpfiles.
    Other than that this has been covered many times do a board search.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    11
    Quote Originally Posted by Stoned_Coder
    you cant compare strings that way you need to use strcmp() to compare strings. look it up in your helpfiles.
    Other than that this has been covered many times do a board search.

    im not doing string comparison, im doing comparison character by character ...

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    no your not!
    Code:
    int isPal(char arr2[])
    {
       char ch[LIM];
       StRev(arr2,ch);
    
       if(ch==arr2)
         return 1;
       else
         return 0;
    
    
    }
    At the moment this code compares the address of the array ch with the address of the array arr2. This as i have already said needs fixing.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int is_palindrom(char niz[]);
    
    int main(int argc, char *argv[])
    {
      if(argc<2)
        { 
          printf("Need an argument-a word!\n");
          exit(1);
        }
    
      if(strlen(argv[1])==0 || strlen(argv[1])==1)
        { printf("input a word not a character!\n");
        exit(1);
        }
    
      if(is_palindrom(argv[1])==0)
        {
          printf("A word %s is palindrom!\n",argv[1]);
        }
    
      else printf("Not a palindrom!\n");
     
    }
    
    int is_palindrom(char niz[])
    {
      char reverse_niz[strlen(niz)];
      int i;
      int x=0;
      for(i=strlen(niz)-1;i>=0;i--)
        {
          reverse_niz[x]=niz[i];
          x++;
        }
      reverse_niz[x]='\0';
         
      if(strcmp(reverse_niz,niz)==0)
        return 0;
    
      else 
    
        return 1;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse member function
    By Aisthesis in forum C++ Programming
    Replies: 13
    Last Post: 05-18-2009, 11:00 AM
  2. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  3. reverse function
    By phoebus in forum C Programming
    Replies: 7
    Last Post: 04-28-2008, 09:38 PM
  4. a reverse function
    By AngKar in forum C Programming
    Replies: 20
    Last Post: 04-27-2006, 10:35 PM
  5. Using A function to reverse the order of an array
    By jaisch in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2005, 04:32 AM