3

This is code I wrote that checks if a string is a palindrome or not. I need to revise this code so that it uses character pointers in it. Could someone give me some suggestions/tips...or show me how to do that? Thanks

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(){
    char string1[20];
    int i, length;
    int flag = 0;
    printf("Enter a string: ");
    scanf("%s", string1);
    length = strlen(string1);
    for(i=0;i < length ;i++){
        if(toupper(string1[i]) != toupper(string1[length-i-1])){
            flag = 1;
            break;
        }
    }
    if (flag) 
        printf("%s is not a palindrome \n\n", string1); 
    else 
        printf("%s is a palindrome \n", string1);

    return 0;
}
Gary Ridgway
  • 71
  • 1
  • 3
  • 6

4 Answers4

2

In your code you use string1[i] to access the current element from the beginning of the string, and string1[length-i-1] to access the current element from the end of the string. You could create two pointers, pb and pe, and then move them toward each other.

To define pointers, use this:

char *pb = &string1[0]; // Or just string1, compiler will convert it to pointer
char *pe = &string1[length-1];

To advance the pointers toward each other, use pb++ and pe--. To see if the pointers have not crossed each other , check that pb < pe. Currently, your program checks the string twice; there's no need to do that - you can stop as soon as pe becomes less than or equal to the pb. To access the character pointed to by the current pointer, use

toupper(*pb) != toupper(*pe)

You can combine the check with advancing the pointers, like this:

toupper(*pb++) != toupper(*pe--)

Note: it is not safe to use %s, because when users enter more characters than fits in your string1 buffer overrun results. You should specify the length of the buffer, like this:

scanf("%19s", string1); // Leave one char for null terminator
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

I'm not sure I completely understand the question, but I think this answers it. You actually are using character pointers. char string1[20] is the same as char *string1. The difference is that you've basically assigned the pointer to a block of memory. You could access the string in this way.

char string[20] = "foo";

printf("%c\n", string[0]);  // will print 'f'
printf("%c\n", *string); // will also print 'f'

printf("%c\n", string[1]); // will print the first 'o'
printf("%c\n", *(string + 1)); // will also print the first 'o'
csnate
  • 1,601
  • 4
  • 19
  • 31
0

with char * it goes like this

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char string1[20];
    int i, length;
    int flag = 0;
    printf("Enter a string: ");
    scanf("%s", string1);
    length = strlen(string1);

    char *start=string1;
    char *end=&string1[length-1];

    //only check upto half


    for(i=0;i <= (length-1)/2 ;i++)
    {
        if(toupper(*(start+i)) != toupper(*(end-i)))
        {
            flag = 1;
            break;
        }
    }
    if (flag) 
        printf("%s is not a palindrome \n\n", string1); 
    else 
        printf("%s is a palindrome \n", string1);

    return 0;
}
InvisibleWolf
  • 917
  • 1
  • 9
  • 22
0

cant we just copy the original string to another array, and then use strrev() to reverse the copied string and then finally compare the original string with the reversed string?

Like this

1.get new string 
2.copy string to new array 
3.reverse the copied string using strrev 
4.use strcmp to check if both are same or not?

this seemed easier (i am a beginner so please correct me if i am wrong)