2014-01-24 145 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