# Thread: Help with array of chars

1. ## 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. 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?

3. Originally Posted by clmoore3
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. 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

You will need to remember the '\0' character at the end of the string.
[/edit]

5. 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. 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. Originally Posted by clmoore3
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. Originally Posted by clmoore3
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. 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.

10. 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};```

11. 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. 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. 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. Don't I need a scanf line to get the users input like scanf("%c", message);

15. Originally Posted by clmoore3
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