I can't get the exact output I somehow get a text in a another language something like that,
#include <stdio.h>
#include <stdlib.h>
int main()
{
char letter;
char military_code[27][10] = {'Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', 'India', 'Juliett', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa', 'Quebec', 'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey', 'X-ray', 'Yankee', 'Zulu'};
printf("Input alphabet\n");
scanf("%s", &letter);
if (letter == 'A' || letter == 'a'){
printf("%s", &military_code[1]);
} else if (letter == 'B' || letter == 'b') {
printf("%s", &military_code[2]);
} else
{
printf("None");
}
return 0;
}
CodePudding user response:
First, you have to put your strings between double quotation marks - "--" .
Moreover , ‘&’ is used to get the address of the variable. C does not have a string type, string is just an array of characters and an array variable stores the address of the first index location(also , military_code[i] for all 0<=i<=n-1 are arrays and not chars). By default the variable itself points to the base address and therefore to access base address of string, there is no need of adding an extra ‘&’.
So we must write military_code[0]
instead of &military_code[0]
The code becomes :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char letter;
char military_code[27][10] = {
"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf",
"Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November",
"Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
"Victor", "Whiskey", "X-ray", "Yankee", "Zulu"};
printf("Input alphabet\n");
scanf("%c", &letter);
if (letter == 'A' || letter == 'a') {
printf("%s", military_code[0]);
}else if (letter == 'B' || letter == 'b') {
printf("%s", military_code[1]);
} else {
printf("None");
}
return 0;
}
I can propose a version that maybe can give more efficient result :
#include <stdio.h>
//There is no need to add the library <stdlib.h>
int main()
{
char letter;
char military_code[27][10] = {
"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf",
"Hotel", "India", "Juliett", "Kilo", "Lima", "Mike", "November",
"Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
"Victor", "Whiskey", "X-ray", "Yankee", "Zulu"};
printf("Input alphabet\n");
scanf("%c", &letter);
if (letter >= 'A'&& letter<='Z') {
printf("%s", military_code[letter-'A']);
}
else if (letter >= 'a' && letter<='z') {
printf("%s", military_code[letter-'a']);
} else {
printf("None");
}
return 0;
}
CodePudding user response:
Single quotes '
are used for character constants, double quotes "
are used for string literals. You seem to be attempting to use single quotes for strings.
Also, in printf
and scanf
, the %s
conversion format specifier is used for printing a string, whereas %c
is used for printing a single character. You seem to be attempting to use %s
to input a single character.
It is also worth noting that in C, indexes are 0-based, not 1-based. Therefore, military_code[0]
contains the string "Alpha"
, military_code[1]
contains the string "Bravo"
.
Another issue is that when using printf
with a %s
conversion format specifier, you must pass an argument of type char *
, which is supposed to be a pointer to the first character of the string. If you write military_code[0]
, then this expression will automatically decay to a pointer to the first element, i.e. to &military_code[0][0]
. However, you are writing &military_code[0]
instead, which is of the wrong type. That expression is a pointer to the entire sub-array, instead of a pointer to its first character.
After fixing these issues, your code should look something like this:
#include <stdio.h>
int main()
{
char letter;
char military_code[27][10] = {
"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot",
"Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike",
"November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra",
"Tango", "Uniform", "Victor", "Whiskey", "X-ray", "Yankee",
"Zulu"
};
printf( "Input alphabet\n" );
scanf( "%c", &letter );
if ( letter == 'A' || letter == 'a' ) {
printf( "%s\n", military_code[0] );
} else if (letter == 'B' || letter == 'b') {
printf( "%s\n", military_code[1] );
} else
{
printf( "None\n" );
}
return 0;
}
This program has the following behavior:
Input alphabet
a
Alpha
Input alphabet
b
Bravo
Note that you can also use a switch
statement instead of a long if
...else if
chain:
switch ( letter )
{
case 'a':
case 'A':
printf( "%s\n", military_code[0] );
break;
case 'b':
case 'B':
printf( "%s\n", military_code[1] );
break;
case 'c':
case 'C':
printf( "%s\n", military_code[2] );
break;
default:
printf( "None\n" );
}
return 0;
}
If you #include <ctype.h>
, you can also use the function toupper
to halve the number of cases:
switch ( (unsigned char)toupper(letter) )
{
case 'A':
printf( "%s\n", military_code[0] );
break;
case 'B':
printf( "%s\n", military_code[1] );
break;
case 'C':
printf( "%s\n", military_code[2] );
break;
default:
printf( "None\n" );
}
return 0;
}
However, even then it would still be a lot of work to write the cases for all 26 letters in the alphabet. Luckily, if you are using a character set in which the letters A
to Z
are contiguous, you don't have to treat each letter in the alphabet individually, but you can simply write this instead:
letter = toupper( (unsigned char)letter );
if ( 'A' <= letter && letter <= 'Z' )
printf( "%s\n", military_code[letter-'A'] );
else
printf( "None\n" );
Here is the full program which uses this trick:
#include <stdio.h>
#include <ctype.h>
int main()
{
char letter;
char military_code[27][10] = {
"Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot",
"Golf", "Hotel", "India", "Juliett", "Kilo", "Lima", "Mike",
"November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra",
"Tango", "Uniform", "Victor", "Whiskey", "X-ray", "Yankee",
"Zulu"
};
printf( "Input alphabet\n" );
scanf( "%c", &letter );
letter = toupper( (unsigned char)letter );
if ( 'A' <= letter && letter <= 'Z' )
printf( "%s\n", military_code[letter-'A'] );
else
printf( "None\n" );
return 0;
}
Note that ISO C does not require that the letters 'A'
to 'Z'
are contiguous. However, on most common character sets, those letters are contiguous. One notable exception is EBCDIC. On that character set, this trick will not work.