void reverse(char * str) {
char * begin = str;
while (*str) {
str++;
}
char * end = --str;
while (begin < end) {
char temp = *begin;
*begin = *end;
*end = temp;
end--;
begin++;
}
}
void main() {
char str[] = "ishant";
reverse(str);
printf("%s", str);
}
Note : In the above program we have created the string using char array thats why we are able to reverse the string but if we will create the string the using char pointer like as following :
char * str = "ishant ";
then it will error because string created using char pointer is stored in the ROM(read only memory ) mean once you create it you cannot apply operation on it. So pay attention when cerating the string in C .
Please comment if you like the above post or find it helpful.
No comments:
Post a Comment