Thread: Newbie? printing an array backwards

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    42

    Newbie? printing an array backwards

    Hello all, newbie here and yes I am trying to do homework. I have spent the last couple days searching the forums trying to learn. I havent been in school in years (lots of years) and I am struggling with some of the concepts. I dont want anyone to do my homework I just want to learn.

    I need to code a program that asks the user to input a line of text and then prints the text in reverse. Here is what I have so far. Any suggestions on what I am missing or what I should go read/study would be appreciated. Am I barking up the wrong tree using strlen?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char text[500];
    	int letters = 0;
    
    	printf("Please enter a line of text.\n");
    	gets(text);
    	
    	letters = strlen(text)-1;
    	while (letters <= 0)
    	{
    		printf("Your reversed text reads:  %s\n", text[letters]);
    		--letters;
    	
    	}
    	
    	
    getchar();
    getchar();
    return 0;

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't you want letter >=0 ??

    gets() is horribly unsafe! Use

    Code:
    fgets(arrayName, sizeof(arrayName), stdin);
    instead. You'll love it.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, you need to get out of the habit of using gets()
    SourceForge.net: Gets - cpwiki

    > Am I barking up the wrong tree using strlen?
    No, but when has a string length been less than zero?

    Here.
    > while (letters <= 0)

    Also, %s does not print single letters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Quote Originally Posted by Salem View Post
    First, you need to get out of the habit of using gets()
    SourceForge.net: Gets - cpwiki

    > Am I barking up the wrong tree using strlen?
    No, but when has a string length been less than zero?

    Here.
    > while (letters <= 0)

    Also, %s does not print single letters.
    Thanks guys! Instructions were to use "gets" function. I do understand the the "while" problem, brain fart on that one.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Complain and point them to the resource. Keep using fgets if they don't fail you. If they still don't change their mind, then you can use gets. But remember never to use it outside school.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Thank you Elysia (and others), I will bring it up. I did read ahead in the book as to why fgets should be used. We aren't actually to the chapter on gets/fgets function but were told to look it up and use "gets" function.

    Program runs but prints only one character on each line preceeded by "Your reversed text reads: ". I assume I have a problem in my "while" loop?

    Here is what I so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char text[500];
    	int letters = 0;
    
    	printf("Please enter a line of text.\n");
    	gets(text);
    	
    	letters = strlen(text)-1;
    	while (letters >=0)
    	{
    		printf("Your reversed text reads:  %c\n", text[letters]);
    		--letters;
    	
    	}
    		
    getchar();
    getchar();
    return 0;
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's what you told it to do.
    The loops reads:

    While letters >= 0 do
    print "Your reversed text reads: "
    print letter at text[letters].
    print newline.
    decrement letters.
    Repeat.

    Rework that logic a little and you'll see it will work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    This prints any string you enter under the specified array size and prints it backwards! Very simplistic, if you havent already got it by now.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       char str1[20];
       int a;
       printf("Please enter a sentence under 20 characters. ");
       fgets(str1, sizeof(str1), stdin);
       for(a = strlen(str1); a > -1; --a) printf("%c", str1[a]);
       return 0;
    }
    Last edited by codecaine_21; 09-19-2010 at 12:24 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't spoil the solution. People learn by trying. If you spoil the solution, they won't learn that part.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    sorry Elsia, but i felt that a working example would give him a better understanding. Wont happen again

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Working examples are good. But also keep in mind that logic is something that newbies need to learn. It is essential.
    So guiding them through it rather than showing a full featured example is usually a better idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    63
    The best way to learn and gain experience definitely would be to mess with code yourself so i completely understand and respect your seniority to this board and your wishes!

  13. #13
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Here's how I would do it.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        int x;
        char y[500];
        fgets(y, sizeof(y), stdin);
        for (x = (strlen(y) - 2); x >= 0; x--)
            putchar(y[x]);
        putchar('\n');
        return 0;
    }
    gets is unsafe. I assigned x to the length of y minus two so it doesn't print the newline character.
    Last edited by Babkockdood; 09-18-2010 at 03:50 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Babkockdood View Post
    ...gets is unsafe...
    That has been pointed out already.
    (Don't you read links or replies?)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by Elysia View Post
    That has been pointed out already.
    (Don't you read links or replies?)
    Yes. Why not remind him once more?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing 2D Array
    By BB89 in forum C Programming
    Replies: 11
    Last Post: 11-16-2009, 05:33 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Printing an Array to the screen
    By simhap in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2001, 11:16 AM