Thread: Help with array of chars

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    Help with array of chars

    The overall program is suppose to take a user inputted sentence / message, and print it out using B1FF style.
    i.e. Input: Hey dude, C is rilly cool
    Output: H3Y DUD3, C 15 R1LLY C00L!!!!!!!!!!
    Program is suppose to convert the message to upper-case letters, substitute digits for certain letters, and then print out 10 or so ! marks. The Hint: Store the original message in an array of characters, then go back through the array, translating and printing characters one by one.

    I don't know how to go about going through each char in the array and printing them one by one. I plan to use if statements before print characters to determine whether to set that char = something else if it needs to be ( e = 3 ).

    Heres my current code:
    Code:
    /*
      Name: Connor Moore
      Date: 1/30/13
      File: ex8-6
      Purpose: This program converts a message the
      user enters into a B1FF style message.
    */
    
    
    #include <stdio.h>
    #include <ctype.h>
    
    #define charMax = 80;
    
    int main(void) {
    
        int c;                 /* int */
        int count;
        char message[80];
        c = getchar();
        count = 0;
    
        printf("Enter message: ");
        while ((count < 80) && (c != EOF)) {    /* don't go over the array size! */
            message[count] = toupper(c);
            ++count;
            c = getchar();     /* get *another* character */
        }
    
        while ((count > 80) && (c !- EOF)) {
        printf()
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Ok, Could I use this for loop to go through each character?
    Code:
        for(i = 0; i < 80; i++) {
        printf("%c", message[i]);
    
        }
    Also, how do I fix my inital loop to get the chars into the array. The max amount of characters in the array is 80. So how does it know when to stop?
    Last edited by clmoore3; 01-30-2013 at 05:33 PM.

  3. #3
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by clmoore3 View Post
    Ok, Could I use this for loop to go through each character?
    Code:
        for(i = 0; i < 80; i++) {
        printf("%c", message[i]);
    
        }

    Before you do that, try compiling your code with what you have and let me know the error message you get, because you have a few things in there that shouldn't be working.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Use a switch statement when saving the characters one by one

    For example:
    Code:
    int ch = toupper(ch);
    switch (ch)
    {
      case 'I':
        message[count] = '1';
        break;
      ...
      default:
        message[count] = ch;
    }
    That way you don't have to print the characters one-by-one, you can use the %s operator in printf

    [edit]
    You will need to remember the '\0' character at the end of the string.
    [/edit]
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    This Compiles fine without any errors. Not to sure where to go in this program thought. I need to get the first loop working like it should, then know how to get and print each character one by one.

    Code:
    /*
      Name: Connor Moore
      Date: 1/30/13
      File: ex8-6
      Purpose: This program converts a message the
      user enters into a B1FF style message.
    */
    
    
    #include <stdio.h>
    #include <ctype.h>
    
    #define charMax = 80;
    
    int main(void) {
        
        int c, i;                 /* int */
        int count;
        char message[80];
        c = getchar();
        count = 0;
    
        printf("Enter message: ");
        while ((count < 80) && (c != EOF)) {    /* don't go over the array size! */
            message[count] = toupper(c);
            ++count;
            c = getchar();     /* get *another* character */
        }
    
        for(i = 0; i < 80; i++) {
        printf("%c", message[i]);
    
        }
        
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Im not familiar with using switch statements in C. We havent gone over them. Once i fix my two loops, the first one getting the characters, the second one printing them. I planned to add if statements into the printing one so if the char is = 'A' then print out a 4, etc for the other letters im suppose to change to numbers.

  7. #7
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by clmoore3 View Post
    This Compiles fine without any errors.
    That's because you have a insidious little bug with your symbolic constant
    Code:
    #define charMax = 80;
    it should be
    Code:
    #define charMax 80
    notice there is no assignment being done in that constant declaration

    then you use in your array declaration like so
    Code:
    char message[charMax];
    I'd fix that first and then come back if you are still having trouble.

  8. #8
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by clmoore3 View Post
    Im not familiar with using switch statements in C. We havent gone over them. Once i fix my two loops, the first one getting the characters, the second one printing them. I planned to add if statements into the printing one so if the char is = 'A' then print out a 4, etc for the other letters im suppose to change to numbers.
    Then your code should look something like this
    Code:
    while ((count < 80) && (c != EOF)) {    /* don't go over the array size! */
            //pseudo code
            c = toupper(c)
            if c is equal to this character
               c = this character
            else if c is equal to this character
               c = this character
     
            message[count] = c;
    
            ++count;
    
            c = getchar();     /* get *another* character */
    
        }
    Try that and see how it works out.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You will need to have a way of indicating the end of the string has been met

    Eventually, the getchar will return a '\n' (new line) -> That is when you need to stop getting input

    Code:
    if (c == '\n')
    {
      /* Exit for loop */
      break;
    }
    You also need to have a finishing point when printing each output -> I suggest that you keep printing until you detect the '\n' character again.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, to avoid accidentally printing garbage, you should initialise your array to all 0's

    Code:
      /* declare "message" and make all elements = 0 */
      char message[80] = {0};
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Ok, now im getting this error for each if and else if statement.
    Error: ex8-6.c:26:2: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define charMax 80;
    
    int main(void) {
        
        int c, i;                 /* int */
        int count;
        char message[80];
        c = getchar();
        count = 0;
    
        printf("Enter message: ");
        while ((count < 80) && (c != EOF)) {    /* don't go over the array size! */
            c = toupper(c);
    
        if(c = 'A') {
            c = 4;
        }
        else if(c = 'B') {
            c = 8;
            }
            else if(c = 'E') {
            c = 3;
        }
        else if(c = 'I') {
            c = 1;
        }
        else if(c = 'O') {
            c = 0;
        }
        else if(c = 'S') {
            c = 5;
        }
    
    
        message[count] = c;
    
            ++count;
    
            c = getchar();     /* get *another* character */
        }
    
        for(i = 0; i < 80; i++) {
    
        printf("%c", message[i]);
    
        }
        
    
        return 0;
    }

  12. #12
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    what you have:
    Code:
    if(c = 'A') {
            c = 4;
        }
    what you should have:
    Code:
    if(c == 'A') {
            c = '4';
        }
    you have to differentiate between the assignment operator
    Code:
    =
    and the equality one
    Code:
    ==
    also you are assigning a character to see so enclose it in single quotes
    Code:
    'char'

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    ok changed the if and else statements to == operands and ' ' around the numbers im changing them to. Program compiles cleanly, but output does this:

    connor@connor-VirtualBox:~$ cd hw04
    connor@connor-VirtualBox:~/hw04$ ./ex8-6

    Enter message: connor@connor-VirtualBox:~/hw04$ ^C
    connor@connor-VirtualBox:~/hw04$

    after i execute it, i have to click enter, then it saus Enter message, and doesnt even let me do anything.

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Don't I need a scanf line to get the users input like scanf("%c", message);

  15. #15
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by clmoore3 View Post
    Don't I need a scanf line to get the users input like scanf("%c", message);
    You're already using getchar()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  2. Saving chars to array
    By ReggieBE in forum C Programming
    Replies: 13
    Last Post: 03-26-2006, 11:11 AM
  3. cin to array of chars
    By cdonlan in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2005, 01:00 PM
  4. Simple question about array of chars
    By Chewy in forum C Programming
    Replies: 9
    Last Post: 04-12-2004, 05:13 AM
  5. Array of pointers (chars)
    By Vber in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 04:29 AM