Location: cprograms

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

Showing 1 post
intel_ram2002
intel_ram2002
String Reversal continue 1-2
Aug 26 2007, 1:21 PM EDT | Post edited: Aug 26 2007, 1:21 PM EDT
char* StrReverse1(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;
}

char* StrReverse2(char* str)
{
int i, j, len;
char temp;
char *ptr=NULL;
i=j=len=temp=0;

len=strlen(str);
ptr=malloc(sizeof(char)*(len+1));
ptr=strcpy(ptr,str);
for (i=0, j=len-1; i<=j; i++, j--)
{
temp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=temp;
}
return ptr;
}

void StrReverse3(char* str)
{
int i, j, len;
char temp;
i=j=len=temp=0;

len=strlen(str);
for (i=0, j=len-1; i<=j; i++, j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}



/*A coooooooooool way of reversing a string by recursion. I found it at this web address
http://www.geocities.com/cyberkabila/datastructure/datastructuresright_reversestring.htm
*/

void StrReverse4(char *str)
{
if(*str)
{
StrReverse4(str+1);
putchar(*str);
}
}
Do you find this valuable?    
Keyword tags: None (edit keyword tags)

Be the first to reply.