【问题标题】:Morse Code Translator in CC中的莫尔斯电码翻译器
【发布时间】:2014-01-24 02:46:33
【问题描述】:

我们应该将字符串翻译成摩尔斯电码,而我已经使用 switch 解决了这个问题。每个字母都用空格分隔,但我不知道如何用斜杠 (/) 分隔单词。这是我的代码:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>

int main(){
char string[100], str1[100];
int length, i, j = 0;

printf("Enter sentence to convert: ");
gets(string);
length = strlen(string);

for (i = 0; i <= length; i++){
    switch(toupper(string[i])){
        case 'A':
            str1[j++]='.';
            str1[j++]='-';
            str1[j]=' ';
            break;

直到 Z 然后...

        }
    j++;
}

str1[j - 1]='\0';
puts(str1);
system("pause");
return 0;
}

如果输入的字符串是句子,如何在单词之间添加斜线?

【问题讨论】:

  • 提示:你如何识别一个单词的结尾和另一个单词的开头?当你看到它时,你想输出斜线。对吗?
  • 您似乎不太可能需要包含 process.h! (而且 conio.h 最好也被排除在外。)

标签: c string morse-code


【解决方案1】:

当您看到一个空格(或一系列空格?)时,将'/' 附加到输出字符串。

【讨论】:

  • 所以不能将它包含在案例中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
相关资源
最近更新 更多