Thread: C programming using gcc compiler

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    5

    C programming using gcc compiler

    Dear all,

    I am trying to do a program using gcc compiler to reverse a string input.

    I am stuck at the for loop which I think there is some mistake there. I only manage to reverse two of the letters only.

    Kindly advise.

    Below is part of my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define size 128
    
    int main(void)
    {
        char line[size];
        printf("Enter line:");
       fgets(line, size, stdin);
    
        int n, i;
        n = strlen(line);
        
       for (i = 0; i > n; ++i)
        {
            line[i] = line[n-1-i];
         }
    
        printf("Your reverse line is %s\n, line);
    
    
       exit(0);
    
     }


    Would really appreciate someone to point out where I have gone wrong.

    Thanks.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Run through your program by hand to see what the loop is doing. It's really obvious if you simply desk-check you program.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
    for (i = 0; i > n; ++i)
    The for loop executes while this condition is true. It is never true.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Somehow i got what you meant already.

    Code:
    for (i = 0; i < n; ++i)
    Only then the condition will be true.

    For the for loop, i should use a separate count to differentiate between the length of the string and count. So that it won't affect the for loop condition.
    and i should not do my code this way.

    Code:
    line[i] = line[n-1-i]
    If my i = 0, i will end up having my array grabbing null into the array
    which i am suppose to grab my character instead.

    So far, am I right to say this way??

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    What happened when you did the desk check?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    The first time i run,
    Code:
    Enter your line:testy
    your reverse is: 
    ytty
    Application Exited.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    second try..

    i got it already.

    Code:
    Build Success
    ------------------------------
    Enter your line:testytesty
    your reverse is: ytsetytset
    Application Exited.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    thanks guys..

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Think about if you enter "test", what are you doing step by step?

    This looks like what you are doing with your algorithm
    Code:
    (Pseudo-ish)
    A[] = {t, e, s, t, \0}
    
    A[0] = A[3] >>> A = {t, e, s, t, \0}
    A[1] = A[2] >>> A = {t, s, s, t, \0}
    A[2] = A[1] >>> A = {t, s, s, t, \0}
    A[3] = A[0] >>> A = {t, s, s, t, \0}
    i.e. You are writing over data that you need.

    [edit]
    You seem to have solved the problem whilst I was typing!
    Good stuff.
    [/edit]
    Last edited by Click_here; 09-13-2012 at 09:59 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Introduce my programming language & compiler
    By blackoil in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 06-29-2012, 10:31 PM
  2. Programming Tools in Turbo C (16-bit Compiler)
    By Vxyz in forum C Programming
    Replies: 40
    Last Post: 08-03-2011, 09:40 AM
  3. Replies: 11
    Last Post: 01-26-2010, 08:00 PM
  4. C Programming Compiler
    By lukesowersby in forum C Programming
    Replies: 15
    Last Post: 03-21-2009, 09:07 AM
  5. Replies: 7
    Last Post: 08-07-2007, 04:00 AM