Thread: Why doesn't it properly display the string?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    Why doesn't it properly display the string?

    The program should store a character array in reverse order then display the reversed array. I have also included in the code that will display the actual characters into the array as it loops through. So I know the characters are being stored, but why doesn't it display the entire string when I call it?

    Code:
    #include<stdio.h>
    int main()
    {
        char str[50];
        char rev[50];
    
        printf("Enter Desired Text: ");
        scanf ("%[^\n]%*c", str); 
    
        int i=0,j=0,k=0;
        while(str[i++]!='\0');
        while(i>=0)
        {
        rev[++j]=str[--i];
        /*Just Double checking
        What should go into the array*/
        printf("%c",rev[j]);
        }
        rev[j]='\0';
        printf("\n");
    
        printf("String: %s ",rev);
        return 0;
    }
    Is it just my compiler?
    Last edited by zerokernel; 06-14-2013 at 10:08 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You are likely reversing the string twice.
    You normally go halfway into a string to reverse once.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by stahta01 View Post
    You are likely reversing the string twice.
    You normally go halfway into a string to reverse once.

    Tim S.
    he does not do in place reversing - so he should copy whole string...

    zerokernel,

    What is the first char you are copying?

    Looks like you are starting with zero character - making your resulting string empty...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by vart View Post
    he does not do in place reversing - so he should copy whole string...

    zerokernel,

    What is the first char you are copying?

    Looks like you are starting with zero character - making your resulting string empty...
    I just noticed something else weird with my count when it reaches the null character. Is it wrong? Lets assume my entered test was "Hello World" and I set "i" initially to be 5. How is it that my value for i reach 12?

    According to what is outputed in my terminal the first character entered to the array is "d" so I'm not sure if it's wrong. Would a terminal output a null character, then continue till it gets to a letter?


    I just tested my code again with a simple printf("\0"); So yes if my program was printing out null characters it wouldn't even display them on the prompt as a space or a return. So my test output for my loop ignores the null characters and then just displays the text. All I see is "dlroW olleH"

    Code:
    #include<stdio.h>
    int main()
    {
        char str[50];
        char rev[50];
    
        printf("Enter Desired Text: ");
        scanf ("%[^\n]%*c", str); 
    
        /*How is this possible?
        I should only get a value less than 12
        assuming the text entered was "Hello World"
        Why is it incrementing i wrong? Is it my scanf?
        */
        int i=5,j=0,k=0;
        while(str[i++]!='\0');
        printf("Value: %d \n",i);
        while(i>=0)
        {
        rev[++j]=str[--i];
        /*Just Double checking
        What should go into the array*/
        printf("%c",rev[j]);
        }
        rev[j]='\0';
        printf("\n");
    
        printf("String: %s ",rev);
        return 0;
    }
    Last edited by zerokernel; 06-14-2013 at 10:53 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can use something like
    Code:
    if(rev[j] != '\0')
    {
       printf("%c",rev[j]);
    }
    else
    {
       printf("\\0");
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
        while(str[i++]!='\0');
    When str[i] == '\0' you are still incrementing "i".
    You need to increment "i" in the body if you don't want to count the '\0'.

    Code:
        rev[++j]=str[--i];
    You increment "j" before assigning a value to "rev[j]", thus you don't store anything in "rev[0]". It probably happens that "rev[0]" is 0 thus you are printing an emtpy string. Try adding
    Code:
    printf("%d\n", rev[0]);
    after the loop to see what's actually there.

    I suggest you read again how post- and pre-incrementing works.

    Bye, Andreas
    Last edited by AndiPersti; 06-15-2013 at 12:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2013, 05:33 AM
  2. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  3. Getting my Sega Genesis to display properly
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-10-2007, 01:40 PM
  4. Loop doesn't seem to function properly
    By TeQno in forum C++ Programming
    Replies: 1
    Last Post: 01-31-2005, 05:25 PM
  5. can't get this one to display properly
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-21-2001, 02:14 AM