My Tech notes: How to reverse a String without using C functions
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Monday, May 12, 2008

How to reverse a String without using C functions

How to reverse a String without using C functions ?
the above is slightly wrong
this is the corrected one..
char * rev_string (char * str)
{
char temp;
int i , j;
for (i = 0 ; str[i]!= '\0' ; i++);
for(j = 0 ; j < i ; j++ , i--)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
}
return str;
}
#include<stdio.h>
void reverse(char *);
void main()
{
char str[]="Hello";
reverse(str);
printf("Reverse String is %s",str);
}
void reverse(char *p)
{
char *q=p;
while(*++q!='\0');
q--;
while(p<q)
{
*p=*p+*q;
*q=*p-*q;
*p=*p-*q;
p++;
q--;
}
}
 blog it

1 comment:

Bhagwat said...

In first case, decrement i before going to loop, ie i--; before compare loop.

Post a Comment