C中小写到大写

时间:2017-02-20 09:30:36

标签: c arrays

(我将通过一些更改再次发布此内容。)我正在创建一个程序,将单词中的所有字母(文本文件中的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;   
}

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

1 个答案:

答案 0 :(得分:3)

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

上述方法是错误的。要访问个人角色,您需要使用

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