0
 #include <stdio.h>
    
    void revstr(char str[])
    {
        char temp;
        int size = 0;
        while(*str != '\0'){
            size++;
            str++;
        }
        for (int i = 0; i < size/2; i++)        
        {
            temp = str[i];               
            str[i] = str[size-1-i];
            str[size-1-i] = temp;
        }
        for(int i = 0; i < size; i++)
        {
            printf("%c\n", *(str+i));
        }
    }
    int main()
    {
        char str[20];
        printf("enter a string: \n");
        scanf("%s", &str);
        revstr(str);
        return 0;
    }

why is my rev string not printing the reverse of the string it is printing out some garbage value. can you point out why?

1
  • 1
    ***void revstr(char str[]) is not cut-n-pasteable code. It is much more useful if you provide code samples that are easy to work with. Commented Jun 10, 2021 at 16:40

3 Answers 3

2

After this while loop

    while(*str != '\0'){
        size++;
        str++;
    }

the pointer str does not point to the beginning of the string.

Instead you could write for example

    while( str[size] != '\0'){
        size++;
    }

Nevertheless such a function should do only one thing: to reverse a string. It is the caller of the function that decides whether to output the reversed string.

So the function can look like

char * revstr( char s[] )
{
    size_t n = 0;

    while ( s[n] ) ++n;

    for ( size_t i = 0; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n - i - 1];
        s[n - i - 1] = c;
    }

    return s; 
}

and in main you could write

puts( revstr( str ) );

Here is a demonstrative program.

#include <stdio.h>

char * revstr( char s[] )
{
    size_t n = 0;

    while ( s[n] ) ++n;

    for ( size_t i = 0; i < n / 2; i++ )
    {
        char c = s[i];
        s[i] = s[n - i - 1];
        s[n - i - 1] = c;
    }

    return s; 
}

int main(void) 
{
    char s[] = "Hello";
    
    puts( s );
    puts( revstr( s ) );
    
    return 0;
}

The program output is

Hello
olleH

Of course instead of the while loop you could use the standard string function strlen.

size_t n = strlen( s );

Pay attention to that in the call of scanf

scanf("%s", &str);

the second argument shall be the expression str

scanf("%s", str);
Sign up to request clarification or add additional context in comments.

5 Comments

in scanf("%s", &str) is also working fine there is no problem in that. the problem was in the while loop where I changed the address of str. Thanks for the ans
@nabeelkhan If something works as expected it does not ,mean that it is correct.
@nabeelkhan In my function there was a typo in the word while. I updated the code.
@nabeelkhan scanf("%s", &str) is absolutely not working. What happens when you do echo 'this is a relatively long string' | ./a.out? (hint, the behavior is undefined). You want if( scanf("%19s", str) == 1 ){ ... }
Whether you use &str or str is, I suppose, a matter of taste, but str is definitely preferred. Eventually, the code will be modified and str will become allocated on the heap, and suddenly all the usages of &str will be wrong.
0

I would implement it another way:

char *reverse(char *str)
{
    char *wrk = str, *end = str;

    if(str && *str)
    {
        while(*(end + 1)) end++;
        while(end > wrk)
        {
            char tmp = *end;
            *end-- = *wrk;
            *wrk++ = tmp;
        }   
    }
    return str;
}
  1. It is good to check if the parameter is correct.
  2. I would return the value. It allows you to use the reversed string in expressions and as a function parameter. Example below:
int main(void)
{
    char s[] = "Hello world";

    printf("%s\n", reverse(s));
}

https://godbolt.org/z/fbo4nsn38

Comments

0

In your code you advance the pointer str to calculate its length but you forgot to reset it to the start after that. Also, your call to scanf should pass str, not its address. Moreover scanf will write beyond the end of str if the user enters a string longer than 19 characters. Try this instead:

#include <stdio.h>
#include <string.h>

void revstr(char str[])
{
    char temp;
    int size = strlen(str);
    for (int i = 0; i < size / 2; i++) {
        temp = str[i];
        str[i] = str[size - 1 - i];
        str[size - 1 - i] = temp;
    }
}

int main()
{
    char str[20];
    printf("enter a string: \n");
    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';
    revstr(str);
    printf("%s\n", str);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.