Results 1 to 4 of 4

Thread: What am I doing wrong? Please help

  1. #1

    Unhappy What am I doing wrong? Please help

    Heres the problem i have: Write a program that reads in a line of input and then prints the line out in reverse order. Recall that you can use scanf() with the %c specifier to read one character at a time from input and that the newline character (\n) is generated when you press the Enter key.

    this is the code i have but it doesnt work. what am i doin wrong. Any help would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
         char ch[255];
         int index, n, i;
    
         for (index = 0; ch[index] != '\n'; index++)
         {
                scanf("%c", &ch[index]);
    
         }
    
         n = strlen(ch);
    
         for (i = n - 1; i >= 0; i--)
         {
                printf("%c", ch[i]);
         }
    
         system ("pause");
    
         return 0;
    }


    Last edited by fallenangel1331; 06-25-2008 at 11:06 PM.

  2. #2
    Junior Member
    Join Date
    May 2008
    Answers
    1

    Re: What am I doing wrong? Please help

    Here i made some modifications to ur prm.
    1.In 1st for loop index value should be decremented,before checking the condition.Bcoz u r doing post increment.
    2.In 2nd for loop im using index value instead of n.i value should point to last element in an array.So i value should be decremented twice(i.e. post increment of i and newline character)

    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
    char ch[255];
    int index, n, i;
    for (index = 0; ch[index-1]!='\n' ;index++)/* Index value is decremented*/
    {
    scanf("&#37;c", &ch[index]);
    }
    // n = strlen(ch);
    for (i = index - 2; i >= 0;i--) /* Here i is made to point last character*/
    {
    printf("%c", ch[i]);
    }
    system ("pause");
    return 0;
    }
    is it ok?
    By
    Mpandi S.


  3. #3

    Talking Re: What am I doing wrong? Please help

    Thank you very much Muthupandi_s it worked great. I really apriciate it. It works great.


  4. #4
    Junior Member
    Join Date
    Jul 2008
    Answers
    2

    Re: What am I doing wrong? Please help

    i think you are checking the condition after incrementing index , a for loop is essentially :
    for (expr1; expr2; expr3)
    statement
    is equivalent to
    expr1;
    while (expr2) {
    statement
    expr3;
    }

    hence you are reading into ch[index] while checking for condition at ch[index+1) , try the following :

    do
    {
    scanf("&#37;c", &ch[index]);
    printf("\n%c", ch[index]);

    }while( ch[index++] != '\n');

    it should work


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact