Thread: 1st CS homework, Almost done!

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    1st CS homework, Almost done!

    So close, yet so far!

    -This is the last part of a long homework set. It asks for a program that:
    -Asks the user to insert a word
    -Reads the string
    -Converts all the characters to lowercase
    -prints the string characters in reverse order

    I had it working up until the last part, reversing the order. Please excuse the horribleness of the follow code, I'm brand new at this:

    Code:
    #include<stdio.h>
    #include<string.h>
    
        int main() {
    char str1[100];
    char name [100];
        printf("Enter your first name:\n");
        fgets(str1, sizeof(str1), stdin);
        sscanf(str1, "%s", name);
    
    int i = 0;
    
        while (i <= strlen(name)){
            if((name[i] >= 'A') && (name[i] <= 'Z'))
                name[i] = name[i] + 32;
                i++;
        }
    char eman[100];
    int y;
    i=0;
        while (i <= strlen(name))  {
            y = strlen(name) - i;
            name[y] = eman[i];
            i++;
        }
    
        printf("%s", eman);
        return(0);
    }

    I feel like I'm on the right track that I need eman[i] to equal name[strlen(name)-i], but having problems representing that effectively.

    Any help is appreciated. Also, any style tips, gonna have to start making the programs from this set look presentable lol

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Style is a factor alright... As it is now your code looks for all the world like cut and paste (scoop and poop) code gathered from all over the place... I'm sure you wouldn't do such a silly thing, but that is the appearance. There are lots of source code formatting guides out there... Google is your friend!

    For your last section... reversing the name... sit down with a piece of paper and do it by hand, character by character... make careful not of how you have to do it... then write the code.
    (Hint: think about the end of one string and the beginning of the other...)

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main() {  //function declarations go all the way to the left hand side
      char str1[100];  //all other functions lines get indented 1 level 
      char name [100]; //each level of indentation is 2-4 char's
      printf("Enter your first name:\n"); //use spaces, not tabs
      fgets(str1, sizeof(str1), stdin);  //in the code
      sscanf(str1, "%s", name);
    
      int i = 0; //blank lines offset blocks of code
                    //like this one - nice! :)
      while (i <= strlen(name)) { //one space before the brace to highlight it
        if((name[i] >= 'A') && (name[i] <= 'Z'))
          name[i] = name[i] + 32;
                i++;  //NOT THIS LOOKS part of the if() statement, but it's not
        i++;  //like THIS!
      }
      char eman[100];
      int y;
      i=0;
      while (i <= strlen(name))  {
        y = strlen(name) - i;
        name[y] = eman[i];
        i++;
      }
    
      printf("%s", eman);
      return(0);
    }
    If I gave you five letter blocks a-e, arranged in abcde order, how would you reverse them, just on the kitchen table?

    Describe that, in detail. Step by step. How are you doing it? (and don't say "by using my fingers", either, you smart-ass!)

    Now write that up in pseudo code - that's the skeleton of your program's logic.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Code:
    #include<stdio.h>
    #include<string.h>
    
        int main() {
    
    char str1[100];
    char name [100];
    
        printf("Enter your first name:\n");
        fgets(str1, sizeof(str1), stdin);
        sscanf(str1, "%s", name);
    
    int i = 0;
    
        while (i <= strlen(name)){
            if((name[i] >= 'A') && (name[i] <= 'Z'))
                name[i] = name[i] + 32;
            i++;
        }
    int y = strlen(name);
    i=0;
        while(i <= strlen(name)) {
        name[i] = name[y];
        i++;
        y--;
    }
        printf("%s", name);
    return(0);
    }
    Is this closer?
    Last edited by ClumsySpelunker; 02-13-2011 at 02:16 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
        printf("Enter your first name:\n");
        fgets(str1, sizeof(str1), stdin);
        sscanf(str1, "%s", name);
    Gets the same result as..
    Code:
        printf("Enter your first name:\n");
        scanf("%s", name);
    The reverising code looks about ok... except it should not be <= strlen ... just less than.
    Remember, C arrays always start at 0 ... thus 0 - 9 is a 10 element array.

    Also you can't assign the values back to the same variable with tihs process... You have to save the reversed string in a second array...
    Last edited by CommonTater; 02-13-2011 at 04:56 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I mis-read your OP. You don't need to reverse the letters in the word, you just need to print them out in reverse order.

    So run your for loop that does the printing, backward through the indeces:

    include your string.h and stdio.h files
    declare your i and stringLength variables as int's

    then
    Code:
    stringLength = strlen(word);
    
    for(i=stringLength; i>-1; i--) 
      print ONE char at the word[i] here

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Here's the latest incarnation. It just returns W32

    Code:
    #include<stdio.h>
    #include<string.h>
    
        int main() {
    
    char str1[100];
    char name [100];
    
        printf("Enter your first name:\n");
        fgets(str1, sizeof(str1), stdin);
        sscanf(str1, "%s", name);
        name[strlen(name)-1] = '\0';
    
    int i = 0;
    
        while (i <= strlen(name)){
            if((name[i] >= 'A') && (name[i] <= 'Z'))
                name[i] = name[i] + 32;
            i++;
        }
    
    char eman[100];
    int y = strlen(name);
    i=0;
    
        for(i=0; i < strlen(name); i++){
        name[i] = eman[y];
        y--;
        }
    
        printf("%s", eman);
    
    return(0);
    }
    scanf something something we're supposed to get used to using fgets and sscanf.

    also we were told strlen basically gets sizeof minus the \0

    So if the input is Henry strlen is going to be 4

    I'm open to anything, you can see above I took out the '=' and it still doesn't work (obviously not because of that.)

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So if the input is Henry strlen is going to be 4
    The way to find these things out is to test them... write little programs, see what these things do...

    For the string HENRY ... strlen will return 5 ... but in the array the characters will occupy positions numbered 0 through 4 ... go ahead count them on your fingers... 0, 1, 2, 3, 4 = 5 items.

    [/quote]
    I'm open to anything, you can see above I took out the '=' and it still doesn't work (obviously not because of that.)[/QUOTE]

    Code:
        name[i] = eman[y];   <---- backwards
    
        eman[y] = name[i];

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Using fgets + sscanf is a smart way to learn it. Good for your teacher! Most students are dragging around this scanf() for user input, like a dead albatross around their necks, and they don't know how to use it very well.

    strlen counts up from the zero index, until it reaches the '\0' char marking the end of the string. Has nothing to do with sizeof(). (which is a good thing, btw). Henry has 5 letters, not 4. Try it.

    1) Why not use the logic I posted to reverse the print out of the word?

    2) If you put comments in your blocks of code, it would help, for example:
    /* changes all uppercase to lowercase letters */
    right above the code that does that

    3) What is the first thing that doesn't work?

    4) sscanf() already puts an end of string char, into whatever it scans and stores. Why add your own end of string char?

    5) Why not print out the word, after each block of code that has made a change? Just for checking purposes. Take it out later, after all is debugged. You're building it step by step through refinement, at this time. You need to know what's happening with output.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    OK! Progress!

    Code:
    #include<stdio.h>
    #include<string.h>
    
        int main() {
    
    char str1[100];
    char name [100];
    
        printf("Enter your first name:\n");
        fgets(str1, sizeof(str1), stdin);
        sscanf(str1, "%s", name);
    
    int i = 0;
    
        while (i <= strlen(name)){
            if((name[i] >= 'A') && (name[i] <= 'Z'))
                name[i] = name[i] + 32;
            i++;
        }
    
    char eman[100];
    int y = strlen(name);
    i=0;
    
        for(i=0; i < strlen(name); i++){
        eman[y] = name[i];
        y--;
        }
    
        eman[strlen(eman)-1] = '\0';
    
        printf("%s", eman);
    
    return(0);
    }
    I would have never guessed that string1[x]=string2[y] order would matter! Thank you CommonTater for that.

    Adak, thanks for you help as well. Maybe using your logic will get rid of the 'W' that's printing at the beginning of the string. I really need to start stylizing and commenting on the eight (!!!) other programs in this homework set. And type out the written stuff and binary logic stuff. I'd hate to, but I'm thinking of settling with what I have.
    This class is no joke!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    YOu need to rework your indenting style... it may make sense to you in this program... but in programs with thousands of lines, it will end up being very hard to read...

    Think of the levels of indentation as maching the nesting of code executions... for example your char name[100]; is *inside* main. therefore it should be indented further than main()...

    Structure your source to show you what's going on in a program....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help about homework
    By agathery in forum C Programming
    Replies: 27
    Last Post: 05-19-2010, 09:17 PM
  2. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  3. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  4. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM