Okay....quick and dirty.....
Code:
#include <stdio.h>
#include <string.h>
char* Rev(char *str);
int main(int argc, char *argv[])
{
char str[] = "Hello World";
char str2[] = "Programming Rules!" ;
printf("First string %s\n",str);
printf("Second sring %s\n",str2);
printf("First string reversed %s\n",Rev(str));
printf("Second sring reversed %s\n",Rev(str2));
printf("First string reversed back %s\n",Rev(str));
printf("Second sring reversed back %s\n",Rev(str2));
return 0;
}
char* Rev(char *str){
int x = strlen(str);
char temp;
int y ,z;
if(x % 2){
for(y = 0, z = x-1; y != z; y++, z--){ // If even length string
temp = str[y];
str[y] = str[z];
str[z] = temp;} }
else {
for(y = 0, z = x-1; y+1 != z; y++, z--){// If odd length string
temp = str[y];
str[y] = str[z];
str[z] = temp;} }
return str;
}
This seems to work....but I cant guarantee....
I tested it with an even length string and an odd length string and its seems happy.....
The array is passed just like a pointer, therefore using this function alters your data (not often too good )
Try test this more and add any refinements......I cant test it properly as I wrote it while on break at work so dont yell if its goes wrong