将 char 指针数组转换为小写 C

标签 c arrays string pointers 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 中的所有字符串设为小写时,上面的代码会导致段错误。如何解决此问题以成功将所有字符串转换为小写

最佳答案

wordpointer != '\0';

这永远不会是真的,因此循环永远不会停止。指针永远不会等于零,除非它回绕,并且在这种情况发生之前你就会崩溃。您的意思是 *wordpointer != '\0';

关于将 char 指针数组转换为小写 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35490575/

相关文章:

python - 在 Python 中替换字符串中的特殊字符

python高效替换嵌套数组中的字符串

python - 填充作为参数传递给 C 函数的 Python 列表

c - C 语言中可以并行执行输入/输出操作吗?

c - 从环境变量生成 #include 宏

vb.net - 如何获得其他2个字符之间的字符?

c - 使用顶点缓冲区和纹理缓冲区的 OpenGL C++ 顺序

c - 2D char 数组或 1D 数组到 char 指针哪个更快?

c++ - copy() 将除最后一个元素之外的所有元素从 vector 复制到数组 C++

arrays - 如何循环遍历 JSONb 字段中包含的数组?