【问题标题】:Getting the last word from a string从字符串中获取最后一个单词
【发布时间】:2014-04-05 18:05:46
【问题描述】:

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

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

空格和搜索定义为:

char *space, *search;

*array 是指向在 main() 中声明的二维数组的指针,我在不同的函数中将值加载到其中。

代码可以运行,但是在提取第一行的最后一个单词后程序崩溃了。在调试时,我收到错误:Projekt 1.exe 中 0x009915B0 处的未处理异常:0xC0000005:访问冲突读取位置 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;
        }
      }
    }
}

【问题讨论】:

  • 你能展示整个循环吗?另外,预期的输入/输出?我需要一些帮助来可视化您的问题。
  • @AndyG 我按照您的要求添加了密码循环。密码应该没问题,在调试时,程序崩溃并显示错误消息,因为它正在从数组的第二行读取最后一个单词。
  • 猜想:你没有为你的字符串分配内存,因此内存冲突(你可能实际上覆盖了堆栈返回地址)。见this
  • 你不想(*array)[i][j] &lt;= 'Z'&lt;= vs &lt;吗?

标签: c arrays


【解决方案1】:

希望对你有帮助

感兴趣的代码

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2011-06-08
    相关资源
    最近更新 更多