【发布时间】:2016-02-18 19:21:30
【问题描述】:
我有以下 char 指针数组:
char* wordArray[ARRAY_MAX];
现在我想把所有的字符都变成小写。我试图通过循环遍历数组然后使用指针将每个字符转换为小写,使用以下代码来做到这一点:
for (i = 0; i < 2000; i++) {
lineoftext2[i] = lineOfText[i]; //lineoftext is just an array of chars
}
words = strtok(lineoftext2, "\n ,-.)");
Word wordPlaceArr[ARRAY_MAX];
int wordNumArr[ARRAY_MAX];
while (words != NULL)
{
wordArray[count] = words;
count++;
words = strtok(NULL, "\n ,-.)"); //Remove punctuation an put into an array of char pointers
}
char* wordpointer; //HERE is where I want to convert all the letters to lowercase
for (i = 0; i < count-1; i++) {
for(wordpointer = wordArray[i]; wordpointer != '\0'; wordpointer+= sizeof(char)) {
*wordpointer = tolower(*wordpointer); //results in seg-fault
}
}
当我尝试将 wordArray 中的所有字符串设为小写时,上面的代码会导致段错误。如何解决此问题以成功将所有字符串转换为小写
【问题讨论】:
-
几个小时前你已经问过同样的问题了!请勿转发问题!
标签: c arrays string pointers char