gpt4 book ai didi

c - C中小写到大写

转载 作者:行者123 更新时间:2023-11-30 20:50:38 25 4
gpt4 key购买 nike

(我再次发布此内容并进行一些更改。)我正在创建一个程序,该程序会将单词中的所有字母(文本文件中的 173528)从小写字母转换为大写字母。这是我的程序

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NWORDS 173528
typedef char String[29];

void
Cap(char *Words[] )
{
int i = 0;

while (Words[i] != '\0') {
Words[i] = Words[i] - 32;
i++;
}
}
void
Initialize(char *Words[])
{
int i;
String word;
char *pch;

for (i = 0; i < NWORDS; i++) {
scanf("%s", word);

pch = malloc(sizeof(char) * (strlen(word) + 1) );

if (pch == NULL) {
printf("Memory is no enough\n");
exit(1);
}

strcpy( pch, word);
Words[i] = pch;
}
}

void
Print(char *Words[])
{
}

void
Free(char *Words[])
{
}

int
main()
{
char *Words[NWORDS];

Initialize(Words);
Cap(Words);
Print(Words);
Free(Words);
return 0;
}

没有编译器错误,但不会显示预期输出。预先感谢您的帮助!

最佳答案

while (Words[i] != '\0') {
Words[i] = Words[i] - 32;

上面的做法是错误的。要访问单个字符,您需要使用

Words[i][j] // i-th word, j-th letter in the word.

关于c - C中小写到大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42340826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com