C 打印正确的莫尔斯电码以输入字母

C 打印正确的莫尔斯电码以输入字母,c,C,我现在需要输出相当于字母数字输入的摩尔斯电码。我检查这一点的条件是一个if循环:我试图用alpha数组的每个元素查看输入数组的每个元素,但似乎从未找到匹配项。我不确定我是否使用了正确的方法。我尝试取消对输入点的引用,并将值与alpha的每个元素进行比较,直到找到匹配项。如果未找到匹配项,则会发生错误 不工作: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdb

我现在需要输出相当于字母数字输入的摩尔斯电码。我检查这一点的条件是一个if循环:我试图用alpha数组的每个元素查看输入数组的每个元素,但似乎从未找到匹配项。我不确定我是否使用了正确的方法。我尝试取消对输入点的引用,并将值与alpha的每个元素进行比较,直到找到匹配项。如果未找到匹配项,则会发生错误

不工作:

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

int main(void)
{

char *morse[] = {"/",
    ".-","-...","-.-.","-..",".","..-.","--","....","..",".---",
    "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
    "..-","...-",".--","-..-","-.--","--..",
    "-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
char *alpha[]= {" ",
    "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", "1","2","3","4","5","6","7","8","9"};


 char *print_array[50];
 int print_array_index = 0;

 char hold[50];
 int hold_index = 0;

 char input[200];
 int i = 0;

 printf("welcome to the Morse translator.\n");
 printf("Enter input: ");
 fgets(input, sizeof(input), stdin);

char *p;
for (p=input; *p !='\0';++p)
{
    *p = toupper(*p);
}

 if (input[0]=='-' || input[0]=='.')
 {


    while (input[i] !='\0') {

        if (input[i] ==' ' || input[i] == '\n')
        {
            hold[hold_index] = '\0';

            bool found = false;

            for (int x = 0; x < sizeof(morse) / sizeof(char *); x++)
            {
                if (strcmp(morse[x], hold) == 0)
                {
                    print_array[print_array_index++] = alpha[x];

                    found = true;

                    break;
                }
            }

            if (!found)
            {
                fprintf(stderr, "invalid Morse code!\n");
            }

            hold_index = 0;
        }
        else
        {
            hold[hold_index++] = input[i];
        }

        i++;
    }

    for (int x = 0; x < print_array_index; x++)
    {
        printf("%s", print_array[x]);

    }

    printf("\n");
}
else if (isalnum(input[0]))
{
    while (input[i]!='\0')
    {
        if (input[i] ==' ' || input[i] == '\n')
        {
            bool found = false;
        for (int x=0; x < sizeof(alpha)/sizeof  (char*);x++)
            {
                if (alpha[x]==input[i])
                {
        print_array    [print_array_index++] = alpha[x];
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                fprintf(stderr, "Invalid input!\n");
            }
            hold_index = 0;
        }
        i++;
    }
    for (int x=0; x < print_array_index; x++)
    {
        printf("%s",print_array[x]);
    }
    printf("\n");
        }           
    return 0;
  }
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
char*morse[]={”/“,
".-","-...","-.-.","-..",".","..-.","--","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--..",
"-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
字符*alpha[]={“”,
“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", "1","2","3","4","5","6","7","8","9"};
字符*打印_数组[50];
int print_array_index=0;
charhold[50];
int hold_索引=0;
字符输入[200];
int i=0;
printf(“欢迎使用莫尔斯翻译程序。\n”);
printf(“输入:”);
fgets(输入,sizeof(输入),标准输入);
char*p;
对于(p=input;*p!='\0';++p)
{
*p=最大值(*p);
}
如果(输入[0]='-'| |输入[0]='.)
{
while(输入[i]!='\0'){
如果(输入[i]=''| |输入[i]='\n')
{
保持[保持索引]='\0';
bool-found=false;
对于(int x=0;xelse if(isalnum(输入[0]){
的一部分应该是

else if (isalnum(input[0])){
    while (input[i]!='\0' && input[i]!='\n'){
        bool found = false;
        for (int x=0; x < sizeof(alpha)/sizeof(char*);x++){
            if (*alpha[x]==input[i]){
                print_array[print_array_index++] = morse[x];
                found = true;
                break;
            }
        }
        if(!found){
            fprintf(stderr, "Invalid input!\n");
        }
        i++;
    }
    for (int x=0; x < print_array_index; x++){
        printf("%s", print_array[x]);
    }
    printf("\n");
}
else if(isalnum(输入[0])){
while(输入[i]!='\0'&输入[i]!='\n'){
bool-found=false;
对于(int x=0;x
感谢您的帮助。我开始了解指针是如何工作的work@Brett主要问题之一是
if(input[i]='''| | input[i]='\n')
:不接受大多数字符。2)
if(alpha[x]==input[i])
:因为
alpha[x]
是一个字符串(
char*
)无法判断,因为它是一个相等的值。它必须是一个字符。3)
print\u array[print\u array\u index++]=alpha[x];
alpha[x]
应该是
morse[x]