Thread: palindromic in C

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    17

    palindromic in C

    I'm trying to write a program to test if a string is palindromic using only pointers
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void revstr(char* str)
    {
           char* t,ch;
           t = str;
           while(*t != '\0')
           {
                   t++;
           }
           t--;
           while(str<t)
           {
                  ch = *str;
                  *str = *t;
                  *t = ch;
                  str++;
                  t--;
           }
    }
    int palin(char* str)
    {
           char* normal = (char*)malloc(sizeof(char)*strlen(str));
           int i;
           for(i=0;i<strlen(str);i++)
           {
                 normal[i]=str[i];
           }
           revstr(str);
           if(strcmp(normal,str) == 0)
                 return 1;
           else 
                return 0;
    }
    int main()
    {
          char* str = (char*)malloc(sizeof(char)*100);
          scanf("%s",&str);
          printf("%d\n",palin(str));
          return 0;
    }
    
    I need to change the arrays in the function int palin to pointers.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    At first glance...
    Code:
          scanf("%s",&str);
    
        //should be
    
    
          scanf("%s", str);
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Don't cast malloc, and make sure you include the proper header for this function (stdlib.h).

    You're also not freeing any of the memory you allocate.

    Code:
    /* warning: format '%s' expects type 'char *', but argument 2 has type 'char **'| */
    
    scanf("%s",&str);
    "scanf()" expects a pointer. "str" is already a pointer (you don't need the '&' operator).

    I need to change the arrays in the function int palin to pointers.
    You're not using any arrays in that function, you're using pointers. You're using array syntax, which is an acceptable way to accessing an offset of a pointer (as long as you don't attempt to access anything outside of the available memory pointed to).

    Even though this is allowed, though, be aware that pointers and arrays are different things.

    Question 6.2
    Question 6.3
    Question 6.4

    It appears, though, that you're just using the array syntax to copy over the string using a loop. Why not just use the "strcpy()" function?

    This leads to two important observations regarding your use of "strlen()". That function does not count the null character. Therefore:

    (1) You aren't allocating enough memory for "normal" to completely hold the contents of "str" (you're one character too short); and

    (2) Even if "normal" had enough space, the loop wouldn't copy the '\0' to "normal" (since '\0' is not counted for the length of the string). Using "strcpy()" instead would correct this problem (assuming you fixed the amount of memory allocated to "normal").
    Last edited by Matticus; 10-16-2014 at 07:07 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jacobhammer View Post
    I need to change the arrays in the function int palin to pointers.
    Strictly speakling, your function palin() has no arrays. It is using pointers as if they are arrays.

    If you mean that you want to eliminate array syntax and use pointer syntax, then the general notion is that array[n] is equivalent to *(array + n).


    As an exercise, you might want to try implementing palin() so it doesn't call any other functions. In other words, don't use malloc(), strlen(), strcmp() or your revstr(). The reason I suggest that is that you will learn more that way than by what you are doing. And you can do it without using array syntax too.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    17
    ahh alright on my review for my test it said not use any array types

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindromic numbers
    By Hodor in forum General Discussions
    Replies: 3
    Last Post: 12-23-2013, 06:20 PM
  2. Palindromic String
    By Laika1986 in forum C Programming
    Replies: 2
    Last Post: 10-29-2011, 12:48 PM
  3. Palindromic numbers problem
    By deadrabbit in forum C Programming
    Replies: 1
    Last Post: 09-27-2011, 08:11 PM
  4. Undefined Behaviour (Palindromic Number Finder)
    By pobri19 in forum C++ Programming
    Replies: 12
    Last Post: 09-28-2008, 04:54 AM
  5. checking if binary numbers are palindromic
    By Beatz in forum C Programming
    Replies: 3
    Last Post: 01-24-2008, 01:49 PM