. strupr C . , , , isupper(), islower() <ctype.h> toupper(), .
. - :
char *str2upper (char *str)
{
if (!str) return NULL;
char *p = str;
for ( ; *p; p++)
if ('a' <= *p && *p <= 'z')
*p += 'A' - 'a';
return str;
}
- , . , , , .
, , fgets. , '\n' , asn. , , , dans higher lower equal - , ?
" "
'H','I','G','H','E','R'
dans, ( '\n', ):
'H','I','G','H','E','R','\n'
fgets '\n', dand . ?
, , ans, fgets, '\n' '\n' nul-terminating character, . ( , , , EOF, . , , ,
if (!fgets (ans, 25, stdin)) {
fprintf (stderr, "user canceled input.\n");
return 1;
}
size_t len = strlen (ans);
if (len && ans[len - 1] == '\n')
ans[--len] = 0;
else
fprintf (stderr, "unread characters may remain in stdin.\n");
strcpy(dans, str2upper(ans));
dans '\n', .
- . ==. ( , ). strcmp string.h . , strcmp 0. , :
printf("The upper string is %s.\n", ans);
if (strcmp (dans, higher) == 0 && (total1 > total))
{
printf("You're right, it is higher!\n");
}
else if (strcmp (dans, lower) == 0 && (total1 < total))
{
printf("You're right, it is lower!\n");
}
else if (strcmp (dans, equal) == 0 && (total1 == total))
{
printf("You're right. they are equal!\n");
}
else
{
printf("Your prediction was wrong.\n");
}
, , , , .
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *str2upper (char *str)
{
if (!str) return NULL;
char *p = str;
for ( ; *p; p++)
if ('a' <= *p && *p <= 'z')
*p += 'A' - 'a';
return str;
}
int main(void)
{
int dice1, dice2, total, total1= 0;
char ans[25] = {0}, dans[25] = {0};
char higher[] = "HIGHER", lower[] = "LOWER", equal[] = "EQUAL";
dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total = dice1 + dice2;
printf("Will the next number be higher, lower or equal to %d ?\n", total);
fputs ("Type higher, lower or equal: ", stdout);
if (!fgets (ans, 25, stdin)) {
fprintf (stderr, "user canceled input.\n");
return 1;
}
size_t len = strlen (ans);
if (len && ans[len - 1] == '\n')
ans[--len] = 0;
else
fprintf (stderr, "unread characters may remain in stdin.\n");
strcpy(dans, str2upper(ans));
dice1 = (rand() % 5) + 1;
dice2 = (rand() % 5) + 1;
total1 = dice1 + dice2;
printf("The upper string is %s.\n", ans);
if (strcmp (dans, higher) == 0 && (total1 > total))
{
printf("You're right, it is higher!\n");
}
else if (strcmp (dans, lower) == 0 && (total1 < total))
{
printf("You're right, it is lower!\n");
}
else if (strcmp (dans, equal) == 0 && (total1 == total))
{
printf("You're right. they are equal!\n");
}
else
{
printf("Your prediction was wrong.\n");
}
}
/
$ ./bin/rolldice
Will the next number be higher, lower or equal to 6 ?
Type higher, lower or equal: lower
The upper string is LOWER.
You're right, it is lower!
, .