read into array and spit out in reverse order

This is a discussion on read into array and spit out in reverse order within the C Programming forums, part of the General Programming Boards category; How do I read my name into an array and spit out my name in reverse order. I can not ...

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

    Thumbs up read into array and spit out in reverse order I cannot get this to run

    How do I read my name into an array and spit out my name in reverse order. I can not get this program to run
    Last edited by steven; 09-06-2001 at 04:08 PM.

  2. #2
    Unregistered
    Guest
    Code:
    #include <stdio.h>
    #define SIZE 50
    
    int main(){
      char array[SIZE];
      int i;
    
      printf("Enter your name");
      scanf("%s", array);
      for(i = SIZE - 1; i > 0; i--){
        printf("%c", array[i]);
      }
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    The above code assumes name will always be 50 characters long, and also first character stored in element 0 wouldnot be printed, would be better to include string.h and use
    for(i = strlen(array) - 1; i >= 0; i--)
    Last edited by bigtamscot; 09-07-2001 at 02:42 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    you could use strrev.

    Code:
    #include<string.h>
    #include<stdio.h>
    
    
    int main()
    {
    	char bud[255];
    
    	printf("Enter a string\n");
    	scanf("%s", bud);
    
    	printf("\nString before reversal: %s\n\n", bud);
    	
    	strrev(bud);
    	printf("\nString after reversal: %s\n", bud);
    
    	return 0;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    That's true bigtamscot, though I was just posting a quick answer to get steven on his way, there are so many ways to do a simple thing in C that it's best to start at the bottom and become comfortable with each level of difficulty in succession.

    WayTooHigh, I wasn't aware there was a strrev() function. You might want to check and see if that's unique to a single compiler or common to all of them, I couldn't find it on my copy of MSVC++.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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