Location: cprograms

Discussion: String Reversal 1-1Reported This is a featured thread

Showing 1 post
intel_ram2002
intel_ram2002
String Reversal 1-1
Aug 26 2007, 1:20 PM EDT | Post edited: Aug 26 2007, 1:20 PM EDT
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char* StrReverse(char*);
char* StrReverse1(char*);
char* StrReverse2(char*);
void StrReverse3(char*);
void StrReverse4(char*);

int main(void)
{

char str[50];
int temp=0;

printf("Enter a string: ");
scanf("%s", str);
printf("The reverse of the string is: %s\n", StrReverse(str));
printf("The reverse of the string is: %s\n", StrReverse1(str));
printf("The reverse of the string is: %s\n", StrReverse2(str));

StrReverse3(str);
printf("The reverse of the string is: %s\n", str);

//Get back the original string
StrReverse3(str);

//Reverse it again
printf("The reverse of the string is: ");
StrReverse4(str);
printf("\n");

scanf("%d", &temp);

}


char* StrReverse(char* str)
{
char *temp, *ptr;
int len, i;

temp=str;
for(len=0; *temp !='\0';temp++, len++);

ptr=malloc(sizeof(char)*(len+1));

for(i=len-1; i>=0; i--)
ptr[len-i-1]=str[i];

ptr[len]='\0';
return ptr;
}

Do you find this valuable?    
Keyword tags: None (edit keyword tags)

Be the first to reply.