#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Program - Reverse given string with poniters
Author - Surendra Patil
*/
int main()
{
char *str,*rev,*ptr,temp;
str = (char *)malloc(10*sizeof(char));
printf("Enter the string \n");
scanf("%s",str);
printf("String before reversing = %s len = %d \n",str,strlen(str));
ptr = str /* point to first character */;
rev = ptr + (strlen(ptr)-1); /* point to last character */
/* keep swapping the characters */
while (ptr < rev) {
temp = *ptr;
*ptr = *rev;
*rev = temp;
ptr++;
rev--;
}
printf("String after reversing = %s\n",str);
return 0;
}
~
#include <string.h>
#include <stdlib.h>
/* Program - Reverse given string with poniters
Author - Surendra Patil
*/
int main()
{
char *str,*rev,*ptr,temp;
str = (char *)malloc(10*sizeof(char));
printf("Enter the string \n");
scanf("%s",str);
printf("String before reversing = %s len = %d \n",str,strlen(str));
ptr = str /* point to first character */;
rev = ptr + (strlen(ptr)-1); /* point to last character */
/* keep swapping the characters */
while (ptr < rev) {
temp = *ptr;
*ptr = *rev;
*rev = temp;
ptr++;
rev--;
}
printf("String after reversing = %s\n",str);
return 0;
}
~
Comments