I am trying to reverse this C-string and I thought I did it correct but the string remains the same when it passes through the function.
#include <stdio.h>
char* reverse(char* string);
int main(int arc, char* argv[]) {
char word[] = "Hello World!";
printf("%s\n", word);
printf("%s\n", reverse(word));
return 0;
}
char* reverse(char* string) {
int i, j, n = 0;int len = 0;char temp;
//Gets string length
for (i = 0; *(string i) != '0'; i ) {
len ;
}
//Reverses string
for (j = len - 1; j >= 0; j--) {
temp = string[n];
string[n] = string[j];
string[j] = temp;
n ;
}
return &string[0];
}
Expected output: Hello World! !dlroW olleH
CodePudding user response:
For starters there is a typo
for (i = 0; *(string i) != '0'; i ) {
You have to write
for (i = 0; *(string i) != '\0'; i ) {
That is instead of the character '0'
you have to use '\0'
or 0
.
In this for loop
for (j = len - 1; j >= 0; j--) {
temp = string[n];
string[n] = string[j];
string[j] = temp;
n ;
}
the string is reversed twice.:) As a result you get the same string.
The function can look for example the following way
char * reverse(char *string)
{
//Gets string length
size_t n = 0;
while ( string[n] != '\0' ) n;
//Reverses string
for ( size_t i = 0, m = n / 2; i < m; i )
{
char temp = string[i];
string[i] = string[n - i - 1];
string[n - i - 1] = temp;
}
return string;
}
Or the function can be defined the following way using pointers
char * reverse(char *string)
{
//Gets string length
char *right = string;
while ( *right ) right;
//Reverses string
if ( right != string )
{
for ( char *left = string; left < --right; left )
{
char temp = *left;
*left = *right;
*right = temp;
}
}
return string;
}
The same approach of the function implementation without using pointers can look the following way
char * reverse(char *string)
{
//Gets string length
size_t n = 0;
while ( string[n] != '\0' ) n;
//Reverses string
if ( n != 0 )
{
for ( size_t i = 0; i < --n; i )
{
char temp = string[i];
string[i] = string[n];
string[n] = temp;
}
}
return string;
}
Here is one more solution. I like it most of all. Tough it is inefficient but it is not trivial as the early presented solutions. It is based on an attempt of one beginner to write a function that reverses a string.:)
#include <stdio.h>
#include <string.h>
char *reverse( char *string )
{
size_t n = 0;
while (string[n]) n;
while (!( n < 2 ))
{
char c = string[0];
memmove( string, string 1, --n );
string[n] = c;
}
return string;
}
int main( void )
{
char string[] = "Hello World!";
puts( string );
puts( reverse( string ) );
}
The program output is
Hello World!
!dlroW olleH
Of course instead of manually calculating the length of a string in all the presented solutions there could be used standard string function strlen
declared in the header <string.h>
.
CodePudding user response:
The problem is that the input word
is an array, which decays to a pointer when passed to the reverse function.
In the for loop, instead of using n
to keep track of the position, I suggest you to use i
and j
to keep track of the start and end of the string, and increment and decrement them respectively and use strlen
to get the length of string.
Also, as it is mentionned above by @Vlad from Moscow, in your for
loop you are checking for 0
but it should be \0
which is the null character.
Please find down below an update of your posted code that is generating the expected result :
#include <stdio.h>
char* reverse(char* string);
int main(int arc, char* argv[]) {
char word[] = "Hello World!";
printf("%s ", word);
printf("%s\n", reverse(word));
return 0;
}
char* reverse(char* string) {
int i, j;
char temp;
int len = strlen(string);
//Reverses string
for (i = 0, j = len - 1; i < j; i , j--) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
return &string[0];
}