String-Handling Functions Using Pointers
Let us see few programs to manipulate strings using pointers.
Example 1:
/* functions to calculate length of a string */
#include <stdio.h>
main()
{
char *ptr, str;
int size=0;
printf ("\n Enter string");
gets(str);
for(ptr=str ; *ptr != '\0' ; ptr ++)
{
size ++;
}
printf ("String length is %d", size);
}
Example 2:
/* function to check for a palindrome */
#include <stdio.h>
main()
{
char str[50], *ptr, *lptr;
printf("\nEnter the string");
gets(str);
for(lptr = str ; *lptr != '\0' ; lptr ++)
/* Reach string terminator */
lptr --; /* Position on the last character */
for(ptr = str ; ptr <= lptr ; lptr--, ptr++)
{
if (*ptr != *lptr)
break;
}
if (ptr > lptr)
printf(" %s is a palindrome");
else
printf("%s is not a palindrome");
}
Copyright © 2008 Manikkumar. All rights reserved.