another method?

This is a discussion on another method? within the C Programming forums, part of the General Programming Boards category; ok for this exercise i had to put in the entered word into an array and print it backwords... i ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    38

    another method?

    ok for this exercise i had to put in the entered word into an array and print it backwords... i was able to do it but in a bad way heres my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char c[20],index,word;
            int i;
    
            printf("Enter a word: ");
            scanf("%s", c);
            word = strlen(c);
            printf("Please enter the word again: ");
            for(i = 0; i <= word; i++)
                    scanf("%c", &c[index]);
            for(i = word; i >= 0; i--)
                    printf("%c", c[i]);
            printf("\n");
            return 0;
    }
    how can i do this without having to enter the word twice?

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
            char c[20],index,word;
            int i;
    
            printf("Enter a word: ");
            scanf("&#37;s", c);
            word = strlen(c);
            /*
            printf("Please enter the word again: ");
            for(i = 0; i <= word; i++)
                    scanf("%c", &c[index]);
            */
            for(i = word-1; i >= 0; i--)
                    printf("%c", c[i]);
            printf("\n");
            return 0;
    }
    Why do you have to enter the word twice?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    to get its word count. so i know how many times to loop through it and add it into the array.

  4. #4
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    I think what NeonBlack was saying is that he commented out part of your code and it works.
    At least if the user's input is sane.
    straight off the heap

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    38
    oh lol i didnt even notice the changes.. lets try it out

    ah ic now.. thanks for the help NeonBlack
    Last edited by me77; 04-19-2008 at 06:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 08:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 08:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21