【问题标题】:Searching array for specific token/string C搜索特定标记/字符串 C 的数组
【发布时间】:2014-01-28 04:48:29
【问题描述】:

这是我现在真正陷入困境的代码。 问题是,用户输入一个摩尔斯电码,然后通过对其进行标记将其存储在一个数组中,现在我的问题是,我将如何在存储每个标记的数组中搜索特定字符串?

例如,我输入:.... . .-.. .-.. --- / .-- --- .-. .-.. -.. 作为我的摩尔斯电码,然后将其标记化。

现在我想在数组中的每个标记中搜索特定字符串,例如:

我将搜索“....”,然后如果它搜索到一个,它将打印出 H,依此类推,直到它形成单词。

字母表中的所有字母和数字也是如此。

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


    #define LIMIT 200
    int main(){
       char morse[LIMIT],*decoded[300];
       char *token;
       int i=0,c;

       printf("Enter morse code: ");
       gets(morse);

       token = strtok(morse, " ");

       while( token != NULL){

        //printf("%s\n",token);
        //strcpy(decoded[i],token);

        decoded[i++] = token;
        token = strtok(NULL, " ");

       }

       if(strcmp(decoded[i],"....")==0){
        printf("HELLO");
       };   
       //for(i=0;i<sizeof(decoded);i++){ 
       //   printf("%s\n",decoded[i]);
       //} 

       system("pause");
       return 0;
    }

编辑

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


#define LIMIT 200
int main(){
char morse[LIMIT],*temp[300],decoded[LIMIT];
char *token;
int i=0,c;

printf("Enter morse code: ");
gets(morse);

token = strtok(morse, " ");

while( token != NULL){

    //printf("%s\n",token);
    //strcpy(temp[i],token);

    temp[i++] = token;
    token = strtok(NULL, " ");

}

for(i=0;temp[i]!='\0';i++){
    if(strstr(temp[i],".-")){
    printf("A");
} else if(strstr(temp[i],"-...")){
    printf("B");
}else if(strstr(temp[i],"-.-.")){
    printf("C");
}
else if(strstr(temp[i],"-..")){
    printf("D");
}
else if(strstr(temp[i],".")){
    printf("E");
}
else if(strstr(temp[i],"..-.")){
    printf("F");
}
else if(strstr(temp[i],"--.")){
    printf("G");
}
else if(strstr(temp[i],"....")){
    printf("H");
}
else if(strstr(temp[i],"..")){
    printf("I");
}
else if(strstr(temp[i],".---")){
    printf("J");
 }
else if(strstr(temp[i],"-.-")){
    printf("K");
}
else if(strstr(temp[i],".-..")){
    printf("L");
}
else if(strstr(temp[i],"--")){
    printf("M");
}
else if(strstr(temp[i],"-.")){
    printf("N");
}
else if(strstr(temp[i],"---")){
    printf("O");
}
else if(strstr(temp[i],".--.")){
    printf("P");
}
else if(strstr(temp[i],"--.-")){
    printf("Q");
}
else if(strstr(temp[i],".-.")){
    printf("R");
}
else if(strstr(temp[i],"...")){
    printf("S");
}
else if(strstr(temp[i],"-")){
    printf("T");
}
else if(strstr(temp[i],"..-")){
    printf("U");
}
else if(strstr(temp[i],"...-")){
    printf("V");
}
else if(strstr(temp[i],".--")){
    printf("W"); 
}
else if(strstr(temp[i],"-..-")){
    printf("X");
} 
else if(strstr(temp[i],"-.--")){
    printf("Y");
}
else if(strstr(temp[i],"--..")){
    printf("Z");
}else if(strstr(temp[i],"/")){
    printf(" ");

}else{
printf("ERROR");
}
//printf("%s",strstr(temp[i],"...."));
}

   //for(i=0;i<sizeof(temp);i++){
   //    printf("%s\n",temp[i]);
   //}      

system("pause");
return 0;
}

【问题讨论】:

  • 在你的第二个代码块中,如果你没有学习过结构,你需要使用结构类型的数组或两个并行数组,这样你就不会写出几乎相同的代码27 次。如果你已经正确标记,你应该使用strcmp() 而不是strstr();您想打印与字符串完全匹配的字母,否则,E 之后包含点的每个字母都显示为 E(幸运的是,T 接近字母表的末尾,但每个字母 U-Z 的摩尔斯电码包含一个破折号,将被视为 T)。
  • 您也没有在strtok() 循环之后分配temp[i] = NULL;,因此您的下一个循环不能保证及时终止。实际上,您可以使用 for (int j = 0; j &lt; i; j++) 之类的循环来代替 for (i = 0; temp[i] != NULL; i++),因为 i 包含正确的数字。

标签: c arrays search token


【解决方案1】:

试试这个---&gt;

  1. 可以使用strstr查找子字符串。
  2. 初始化计数索引以查找位置,

【讨论】:

  • 成功了!现在我只需要修复这个由 for 循环引起的运行时错误。
  • 很高兴能帮上忙。 ---&gt;应该检查count的值。
  • 你遇到了一个新问题,它在识别具有相同模式样式的代码时存在问题。例如,M 是 --- 而 O 是 -- 如果我写了 -- 它仍然会打印出 M
  • 好的。将字符串分解为 ----- 等。然后使用 strcmp 比较等效字符。 ##strpbrk也值得研究
  • 它成功了,但是输出结果后出现运行时错误,必须来自for(i=0;temp[i]!='\0\;i++)
【解决方案2】:

工作代码:

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

#define LIMIT 200

int main(void)
{
    char morse[LIMIT];
    char *temp[LIMIT];
    char *token;
    char *str;
    int i = 0, j;
    struct Morse { char code[8]; char letter; };
    struct Morse alphabet[] =
    {
        { ".-",   'A' },
        { "-...", 'B' },
        { "-.-.", 'C' },
        { "-..",  'D' },
        { ".",    'E' },
        { "..-.", 'F' },
        { "--.",  'G' },
        { "....", 'H' },
        { "..",   'I' },
        { ".---", 'J' },
        { "-.-",  'K' },
        { ".-..", 'L' },
        { "--",   'M' },
        { "-.",   'N' },
        { "---",  'O' },
        { ".--.", 'P' },
        { "--.-", 'Q' },
        { ".-.",  'R' },
        { "...",  'S' },
        { "-",    'T' },
        { "..-",  'U' },
        { "...-", 'V' },
        { ".--",  'W' },
        { "-..-", 'X' },
        { "-.--", 'Y' },
        { "--..", 'Z' },
        { "/",    ' ' },
    };
    enum { NUM_MORSE = sizeof(alphabet)/sizeof(alphabet[0]) };

    printf("Enter morse code: ");
    if (fgets(morse, sizeof(morse), stdin) == 0)
        return 0;
    if ((token = strchr(morse, '\n')) != NULL)
        *token = '\0';

    str = morse;
    while ((token = strtok(str, " ")) != NULL)
    {
        temp[i++] = token;
        //printf("%s\n", token);
        str = NULL;
    }
    temp[i] = NULL;

    for (i = 0; temp[i] != NULL; i++)
    {
        //printf("%-6s ", temp[i]);
        for (j = 0; j < NUM_MORSE; j++)
        {
            if (strcmp(temp[i], alphabet[j].code) == 0)
            {
                putchar(alphabet[j].letter);
                break;
            }
        }
        //putchar('\n');
    }
    putchar('\n');

    return 0;
}

例子:

Enter morse code: .... . .-.. .-.. --- / .-- --- .-. .-.. -..
HELLO WORLD

如果您愿意,您可以改进线性搜索,但这可能不值得。一种选择是按字母在普通英语中出现的频率顺序进行排序(因此 E 在 T 之前,等等);另一个是排序和使用二分查找。

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 2023-03-28
    • 2014-05-27
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2020-09-29
    相关资源
    最近更新 更多