Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 比较字符和字符串(两个数组)并打印结果时遇到问题吗?_C - Fatal编程技术网

C 比较字符和字符串(两个数组)并打印结果时遇到问题吗?

C 比较字符和字符串(两个数组)并打印结果时遇到问题吗?,c,C,作为我大学课程的一部分,我正在做一个编程模块的介绍,而我们最近的项目让我很为难。基本上,用户需要输入一个句子,然后翻译成摩尔斯电码。我在比较用户输入的文本和摩尔斯电码时真的被卡住了。任何帮助都将不胜感激,谢谢 #include <stdio.h> #include <string.h> int main() { char alphabet[27] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',

作为我大学课程的一部分,我正在做一个编程模块的介绍,而我们最近的项目让我很为难。基本上,用户需要输入一个句子,然后翻译成摩尔斯电码。我在比较用户输入的文本和摩尔斯电码时真的被卡住了。任何帮助都将不胜感激,谢谢

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

int main()
{
    char alphabet[27] = { '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', '\0' };
    char morse[26][5] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
    char string[256];
    printf("Please enter a sentence to be converted to IMC: \n");
    scanf("%s", string);
    string[strlen(string)] = '\0';

    for (int i = 0; i < (int)strlen(string); i++)
    {

            if (strcmp (string, alphabet))
            {
                printf("%s ", *(morse + i));
            }
    }

} 
#包括
#包括
int main()
{
字符字母表[27]={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','\0'};
char-morse[26][5]={“-”、“-…”、“-…”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”、“-”;
字符串[256];
printf(“请输入要转换为IMC的句子:\n”);
scanf(“%s”,字符串);
字符串[strlen(字符串)]='\0';
对于(int i=0;i<(int)strlen(字符串);i++)
{
if(strcmp(字符串、字母表))
{
printf(“%s”,*(摩尔斯+i));
}
}
} 

用fgets读你的台词。是否确实要比较strcmp(字符串、字母表)?我想你的意思是

for (int i = 0; i < strlen(string); i++)
{
    for (int j = 0; j < sizeof(alphabet); j++)
    {
            if (string[i] == alphabet[j])
            {
                printf("%s ", *(morse + j));
            }
    }
}```
for(int i=0;i
谢谢!这起作用了。我以前也试过类似的方法,但还没有完全找到正确的方法来放下它。非常有用<代码>字符串[strlen(字符串)]='\0'将“\0”存储在“\0”已存在的确切位置。在C语言中,字符串以“\0”结尾,没有什么比存储在某处的长度更合适的了
strlen()
只计算“\0”前面的字符数。