You want to compare the variable named a, right? Must be
if (a >= 'A' ...
The error happens in the printf line. You can’t just pass anything to printf like you do with Python. It needs a string, possibly with placeholders for other things (“format string”). The correct way to print your int a is:
printf("%d", a);
You might want to read up on printf and format strings, here’s an internet version of the man page:
Thank you. It seems even when it runs without error, I don’t get any output. I don’t understand how to:
1. Instruct 'C' how to tell if letter is upper or lower case without explicitdly making a case for each letter.
2. Once properly identifying a letter’s case, how to use math to change it to the opposite case.`
Do you have any recommendations that explain the above concept?
I’ve tried this as well:
#include<stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2){
printf("ERROR: You need one argument.\.");
//this is how you abort a program
return 1;
}
int i = 0;
for (i = 0; argv[1][i] != '\0'; i++) {
char letter = argv[1][i];
switch (letter){
case i >= 'A':
letter = letter +32;
printf("%d:" ,i);
break;
case i <= 'Z':
letter = letter + 32;
printf("%d:" i);
break;
default:
printf("%d: %c is not an upper case.\n", i , letter);
}
}
return 0;
}
But that results in the following:
C:\Users\jordan.howell\Box\C programs\ex10ec.c [Error] case label does not reduce to an integer constant That was line 16.
A character is just an integer. Letters are mapped to numbers according to the ASCII encoding scheme. So to test if a char is an uppercase letter you test if it is within that region:
char c = some_char();
if (c >= 65 && c <= 90) {
In the table you can see that the difference between an uppercase letter and the corresponding lowercase letter is always 32: 'A' == 65, 'a' == 97. So if the test succeeds, you can manipulate the char – remember, just an integer – like any normal int:
c += 32;
}
printf("%c", c);
Switch statements only work with integer/character constants, not with boolean expressions.
Thanks. My program runs now and that’s progress! I’m not sure how to get it to dynamically print out whatever letter it comes upon. When I run the command:
ex10 AEIOUDNinfaddasf, I get the following:
33: @ is not a vowel.
34: @ is not a vowel.
This is my program.
#include<stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2){
printf("ERROR: You need one argument.\n.");
//this is how you abort a program
return 1;
}
int i = 0;
for (i = 0; argv[1][i] != '\0'; i++) {
char letter = argv[1][i];
switch (letter){
case 'A':
i = i + 32;
printf("%c:\n", i);
break;
case 'E':
i = i + 32;
printf("%c:", i);
break;
case 'I':
i = i + 32;
printf("%c:", i);
break;
case 'O':
i = i + 32;
printf("%c:", i);
break;
case 'U':
i = i + 32;
printf("%c:\n", i);
break;
case 'Y':
if (i > 2) {
//it's only sometimes Y
printf("%c:\n", i);
}
break;
default:
printf("%d: %c is not a vowel.\n", i);
}
}
return 0;
}
You’re mixing up the index variable i and letter. You need to add 32 to the latter. Right now you’re manipulating the index within the loop, so it ends up pointing into raw memory where it happens to find @s.
Also, what’s with that switch statement? You don’t need it, you can do this with a single if statement. Try replacing the whole switch statement with my snippet above (substitute letter for c). That should do the trick already.
I think I got it…or at least what I care about and that’s changing from upper to lower case.
#include<stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2){
printf("ERROR: You need one argument.\n.");
//this is how you abort a program
return 1;
}
int i = 0;
for (i = 0; argv[1][i] != '\0'; i++) {
char letter = argv[1][i];
switch (letter){
case 'A':
letter = letter + 32;
printf("%c:\n", letter, i);
break;
case 'E':
letter = letter + 32;
printf("%c:", letter, i);
break;
case 'I':
letter = letter + 32;
printf("%c:", letter, i);
break;
case 'O':
letter = letter + 32;
printf("%c:", letter, i);
break;
case 'U':
letter = letter + 32;
printf("%c:\n", letter, i);
break;
case 'Y':
if (i > 2) {
//it's only sometimes Y
printf("%c:\n", letter, i);
}
break;
default:
printf("%c is not a vowel.\n", letter, i);
}
}
return 0;
}