使用 topper 函数
Using toupper function
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#define SIZE 5
void select_word(const char *const art[], const char *const noun[],
const char *const verb[], const char *const prep[]);
void build_sentence(const char *const sentence[]);
int main()
{
int i;
const char *art [SIZE] = { "the", "a", "one", "some", "any",};
const char *noun[SIZE] = { "boy", "girl", "dog", "town", "car",};
const char *verb[SIZE] = { "drove","jumped", "ran", "walked", "skipped",};
const char *prep[SIZE] = { "to", "from", "over", "under", "on",};
srand(time(0));
for(i=0; i<3; i++)
select_word( art, noun, verb, prep );
return 0;
}
void select_word(const char *const art[], const char *const noun[],
const char *const verb[], const char *const prep[])
{
const char *sentence [6] = {
art [rand() % SIZE],
noun [rand() % SIZE],
verb [rand() % SIZE],
prep [rand() % SIZE],
art [rand() % SIZE],
noun [rand() % SIZE]};
build_sentence(sentence);
}
void build_sentence (const char *const sentence[]) {
printf( "%s %s %s %s %s %s. \n",
sentence [0],
/* 我正在尝试使用 'toupper' 函数使随机生成的句子中的第一个单词成为大写。我想我在句子 [0] 和句子 [1] 之间放了一个 printf 函数 */
sentence [1],
sentence [2],
sentence [3],
sentence [4],
sentence [5]);
}
您将无法就地使用它来处理字符串的问题,因为它们是常量。您将需要为结果使用一些可写存储。如果你不想,你可以只将第一个单词的第一个字符大写,然后在 printf
:
中以第二个字符开始写第一个单词
printf( "%c%s %s %s %s %s %s. \n",
toupper(sentence[0][0]),
&sentence[0][1],
sentence[1],
..........
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#define SIZE 5
void select_word(const char *const art[], const char *const noun[],
const char *const verb[], const char *const prep[]);
void build_sentence(const char *const sentence[]);
int main()
{
int i;
const char *art [SIZE] = { "the", "a", "one", "some", "any",};
const char *noun[SIZE] = { "boy", "girl", "dog", "town", "car",};
const char *verb[SIZE] = { "drove","jumped", "ran", "walked", "skipped",};
const char *prep[SIZE] = { "to", "from", "over", "under", "on",};
srand(time(0));
for(i=0; i<3; i++)
select_word( art, noun, verb, prep );
return 0;
}
void select_word(const char *const art[], const char *const noun[],
const char *const verb[], const char *const prep[])
{
const char *sentence [6] = {
art [rand() % SIZE],
noun [rand() % SIZE],
verb [rand() % SIZE],
prep [rand() % SIZE],
art [rand() % SIZE],
noun [rand() % SIZE]};
build_sentence(sentence);
}
void build_sentence (const char *const sentence[]) {
printf( "%s %s %s %s %s %s. \n",
sentence [0],
/* 我正在尝试使用 'toupper' 函数使随机生成的句子中的第一个单词成为大写。我想我在句子 [0] 和句子 [1] 之间放了一个 printf 函数 */
sentence [1],
sentence [2],
sentence [3],
sentence [4],
sentence [5]);
}
您将无法就地使用它来处理字符串的问题,因为它们是常量。您将需要为结果使用一些可写存储。如果你不想,你可以只将第一个单词的第一个字符大写,然后在 printf
:
printf( "%c%s %s %s %s %s %s. \n",
toupper(sentence[0][0]),
&sentence[0][1],
sentence[1],
..........