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

时间:2016-02-18 19:21:31

标签: c arrays string pointers char

我有以下数组的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中的所有字符串设置为小写时,上面的代码会导致seg-faults。如何修复此问题以将所有字符串转换为小写成功

3 个答案:

答案 0 :(得分:3)

wordpointer != '\0';

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

答案 1 :(得分:1)

您要做的是要求您检查每个字符串中的每个字符是否由wordArray isupper指向,然后转换字符tolower。你离我不远。以下示例为您的方法提供了完成:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

enum { WORD_MAX = 64, ARRAY_MAX = 256 };

int main (void) {

    char *wordArray[ARRAY_MAX] = {NULL};
    char line[WORD_MAX] = "";
    char *p, *delims = " \t\n;,.";
    size_t i, idx = 0;

    while (fgets (line, WORD_MAX, stdin)) { /* read line/tokenize */
        for (p = strtok (line, delims); p; p = strtok (NULL, delims))
            wordArray[idx++] = p;

        if (idx == ARRAY_MAX)   /* check against max */
            break;
    }

    for (i = 0; i < idx; i++) { /* convert to lower and output */
        printf (" wordArray[%2zu] : %-8s => ", i, wordArray[i]);
        p = wordArray[i];
        while (*p) {
            if (isupper (*p))
                *p = tolower (*p);
            p++;
        }
        printf ("%s\n", wordArray[i]);
    }

    return 0;
}

使用/输出

$ echo "Joe tOM FRANK haRRY" | ./bin/fgets_ptrarray_tolower
 wordArray[ 0] : Joe      => joe
 wordArray[ 1] : tOM      => tom
 wordArray[ 2] : FRANK    => frank
 wordArray[ 3] : haRRY    => harry

如果您有任何问题,请与我们联系。

答案 2 :(得分:0)

以下代码可以满足您的需求......

它利用了C中char实际上是整数的事实,在ASCII表中,每个小写字符都可以表示为UPPER_CASE_LETTER+32

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *foo(char *word){
    char *tmp = word; /* For the new string */
    char *ptr = word; /* To save the poiner */

    for(; *ptr; ++ptr, ++tmp) {
        if (*ptr >= 'a' && *ptr <= 'z')
        /* char is lower case */
            *tmp = (*ptr) - 32;
        else if (*ptr >= 'A' && *ptr <= 'Z')
        /* char is upper case */
            *tmp = (*ptr) + 32;
        else
            *tmp = *ptr;
    }
    return word;
}

int main(int argc, char **argv)
{
    char line[]="This is a test LiNe FOR You";
    char *buf;

    printf("%s\n",foo(strtok(line," ")));

    while(buf=strtok(NULL," "))
        printf("%s\n",foo(buf));

    return 0;
}

可以在几个方面改进代码,例如添加一些边界检查代码,或者在调用strtok()之前进行额外检查

代码输出

$ ./lower_upper.exe
tHIS
IS
A
TEST
lInE
for
yOU