我们应该将字符串翻译成摩尔斯电码,而我已经使用 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;
}
如果输入的字符串是一个句子,如何添加斜杠来分隔单词?
每当您看到一个空格(或一系列空格?)时,将 '/'
附加到输出字符串。
我是一名优秀的程序员,十分优秀!