在赋值中,我需要能够从二维字符串数组中找到最后一个单词(每行/字符串的最后一个单词),这样我就可以对它应用密码。我试过这个:

space = strrchr((*array)[i], ' ');
if ((name = strstr((*array)[i], search)) != NULL) {
    cipher = 1;
    }
if (cipher && name > space) {
 //Cipher code here

空间和搜索定义为:
char *space, *search;

*数组是指向在main()中声明的二维数组的指针,我在另一个函数中将值加载到该数组中。
代码工作了,但是程序在完成第一行的最后一个字后崩溃了。调试时,在Projekt 1.exe中的0x009915B0处出现错误:未处理异常:0xc000005:访问冲突读取位置0xCCCCCCCC。第一行中的单词被正确地提取出来,密码就工作了(我试着在每行之后都打印出来)。
我试过谷歌搜索,也咨询过StackOverflow的其他话题,但对我没什么帮助。有谁能给我指一个正确的方向,告诉我如何得到每一行的最后一个字吗?
编辑:在这种情况下,我使用的是Albam密码:
for (i = 0; i < lines; i++) {

space = strrchr((*array)[i], ' ');
if ((name = strstr((*array)[i], search)) != NULL) {
    cipher = 1;
    }

if (cipher && name > space) {

    cipher = 1;
    for (j = 0; j < strlen(*array[i]); j++) {

        if ((*array)[i][j] < 'Z' && (*array)[i][j] > 'A' || (*array)[i][j] < 'z' && (*array)[i][j] > 'a') {
          if (toupper((*array)[i][j]) > 'A' && toupper((*array)[i][j]) < 'N')
              (*array)[i][j] = (*array)[i][j] + 13;
          if (toupper((*array)[i][j]) > 'N' && toupper((*array)[i][j]) < 'Z')
              (*array)[i][j] = (*array)[i][j] - 13;
        }
      }
    }
}

最佳答案

希望这有帮助
兴趣代码

for(i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
        {

            lastWord = strrchr(array[i][j], ' ');
            if (lastWord == NULL)
                lastWord = array[i][j];
            else
                lastWord++;
            printf("%s\n", lastWord);
            lastWord = NULL;

        }
    }

完整代码
int i,j;
char *line = NULL;
int read, len;
char*** array;
char* lastWord;
int rows, cols;

FILE* fp;
fp = fopen("input.txt", "r");


if(fp != NULL)
{
    // Read the number of rows and cols
    line = NULL;
    read = getline(&line, &len, fp);
    sscanf(line, "%d,%d", &rows, &cols);

    // allocate memory and read the input strings
    array = (char***)malloc(sizeof(char**) * rows);
    for(i=0; i<rows; i++)
    {
        array[i] = (char**)malloc(sizeof(char**) * cols);
    }

    for(i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
        {
            read = getline(&line, &len, fp);
            assert((read >= 0) && (read <= 1024) && (line != NULL));

            array[i][j] = (char*)malloc(read+1);
            strncpy(array[i][j], line, read-1);
        }
    }

    if(line != NULL)
    {
        free(line);
        line = NULL;
    }

    for(i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
        {

            lastWord = strrchr(array[i][j], ' ');
            if (lastWord == NULL)
                lastWord = array[i][j];
            else
                lastWord++;
            printf("%s\n", lastWord);
            lastWord = NULL;

        }
    }


    for(i=0; i<rows; i++)
        for(j=0; j<cols; j++)
            free(array[i][j]);
    for(i=0; i<rows; i++)
        free(array[i]);
    free(array);
}
else
{
    printf("Unable to open the file\n");
}

fclose(fp);

return 0;

关于c - 从字符串中获取最后一个单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22884694/

10-14 10:54
查看更多