2014-01-24 147 views
1

我们应该将一个字符串转换成莫尔斯电码,我已经使用switch了。每个字母用空格分隔,但我不知道如何用斜线(/)分隔单词。这里是我的编码:摩尔斯电码转换器C

#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; 
} 

如何添加分隔单词,如果输入的字符串是一个句子的斜线?

+1

提示:你如何识别一个单词的结尾和另一个单词的开头?当你看到这一点时,你想输出斜线。对? – keshlam

+1

你不需要包含process.h! (conio.h也是最好的。) – ooga

+0

明白了!感谢球员 – confusedcat

回答

2

每当看到空格(或空格序列?)时,将'/'附加到输出字符串。

+0

如此生病包括在案件? – confusedcat

+0

yizz我现在明白了,谢谢! – confusedcat