c - Reverse characters word in array -
for exemple need invers "paris" "sirap"...
my main:
int main(void) { char w1[] = "paris"; reverseword(w1); printf("the new word is: %s",w1); return0; }
and function:
void reverseword(char *str) { int counter=0; for(int i=0; *(str+i)!='\0'; i++) counter++; int length = counter-1; char temp[length]; for(int j=0; temp[j]=='\0'; j++) temp[j]=str[length-j]; }
now have renverse word in temp[]. need put in pointer *str. how can it??
thanks
if want use temp
must function this
void reverseword(char *str) { int i,j; if(str) { int length=strlen(str); char temp[length+1]; for( j=0; j<length; j++) temp[j]=str[length-1-j]; temp[j]='\0'; strcpy(str,temp); } }
without using temp
follows
void reverseword(char *str) { int end= strlen(str)-1; int start = 0; while( start<end ) { str[start] ^= str[end]; str[end] ^= str[start]; str[start]^= str[end]; ++start; --end; } }
Comments
Post a Comment