Arrays 英语句子催眠

Arrays 英语句子催眠,arrays,c,string,Arrays,C,String,我正在编写一个关于单词断字的程序,但我想将其变成一个句子断字。代码是: #include <stdio.h> #include <stdlib.h> #include <stdio.h> int isVowel(char); int main() { char word[50] = "", wordHyp[100] = ""; int i = 0; int count = 0; pri

我正在编写一个关于单词断字的程序,但我想将其变成一个句子断字。代码是:

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>

int isVowel(char);

int main() {
    char word[50] = "", wordHyp[100] = "";
    int i = 0;
    int count = 0;
    printf("Enter a word for hyphenation:\n");
    fgets(word,sizeof(word),stdin);

    for (i = 0; word[i] != '\0'; i++) {
        if (wordHyp[i] == '\0') {
            wordHyp[i] = word[i];
            count += 1;
        } else {
            wordHyp[count] = word[i];
            count += 1;
        }
        if (isVowel(word[i]) == 1) {
            if (isVowel(word[i + 1]) == 1) {
                wordHyp[count] = '-';
                count += 1;
            } else if (isVowel(word[i + 2]) == 1) {
                wordHyp[count] = '-';
                count += 1;
            } else if (isVowel(word[i + 3]) == 1) {
                wordHyp[count] = word[i + 1];
                count += 1;
                wordHyp[count] = '-';
                count += 1;
                i++;
            } else if (isVowel(word[i + 4]) == 1) {
                wordHyp[count] = word[i + 1];
                wordHyp[count + 1] = word[i + 2];
                wordHyp[count + 2] = '-';
                count += 3;
                i += 2;
            }
        }
    }
    printf("%s\n", wordHyp);
    return 0;
}


int isVowel(char c) {

    switch (c) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return 1;
        default:
            return 0;
    }
    return 0;
}
#包括
#包括
#包括
int是元音(char);
int main(){
char-word[50]=“”,wordHyp[100]=“”;
int i=0;
整数计数=0;
printf(“输入一个用于连字符的单词:\n”);
fgets(word、sizeof(word)、stdin);
for(i=0;单词[i]!='\0';i++){
如果(wordHyp[i]='\0'){
wordHyp[i]=单词[i];
计数+=1;
}否则{
wordHyp[count]=单词[i];
计数+=1;
}
如果(是元音(单词[i])==1){
如果(是元音(单词[i+1])==1){
wordHyp[count]='-';
计数+=1;
}else if(Is元音(单词[i+2])==1){
wordHyp[count]='-';
计数+=1;
}else if(Is元音(单词[i+3])==1){
wordHyp[count]=word[i+1];
计数+=1;
wordHyp[count]='-';
计数+=1;
i++;
}else if(Is元音(单词[i+4])==1){
wordHyp[count]=word[i+1];
wordHyp[count+1]=单词[i+2];
wordHyp[count+2]='-';
计数+=3;
i+=2;
}
}
}
printf(“%s\n”,wordHyp);
返回0;
}
int是元音(字符c){
开关(c){
案例“a”:
案例“e”:
案例“i”:
案例“o”:
案例“u”:
返回1;
违约:
返回0;
}
返回0;
}
输入示例:

美国大学

输出:

a-me-ri-can-u-ni-ver-sity

预期产出:

a-me-ri-can u-ni-ver-sity

非常感谢您的帮助:D

试试这个(我在评论中标记了更改的行):

intmain(){
char-word[50]=“”,wordHyp[100]=“”;
int i=0;
整数计数=0;
printf(“输入一个用于连字符的单词:\n”);
fgets(word、sizeof(word)、stdin);
for(i=0;单词[i]!='\0';i++){
如果(wordHyp[i]='\0'){
wordHyp[i]=单词[i];
计数+=1;
}否则{
wordHyp[count]=单词[i];
计数+=1;
}
if(是元音(单词[i])){

如果(单词[i+1]&&is元音(单词[i+1]){//你对这个任务有什么问题?我想让它成为预期的输出你的程序不会产生
a-me-ri-can-u-ni-ver-sity
,而只产生
a-me-ri-can
,原因很简单
scanf(“%s”,word)
停在空格上,因此只能读取
美式
为什么
sity
不能被剪切为具有
sity
?很抱歉,扫描f更改为fgets(word、sizeof(word)、stdin)
int main() {
    char word[50] = "", wordHyp[100] = "";
    int i = 0;
    int count = 0;
    printf("Enter a word for hyphenation:\n");
    fgets(word,sizeof(word),stdin);

    for (i = 0; word[i] != '\0'; i++) {
        if (wordHyp[i] == '\0') {
            wordHyp[i] = word[i];
            count += 1;
        } else {
            wordHyp[count] = word[i];
            count += 1;
        }
        if (isVowel(word[i])) {
            if (word[i+1] && isVowel(word[i + 1])) { //<==
                wordHyp[count] = '-';
                count += 1;
            } else if (word[i+2] && isVowel(word[i + 2]) && word[i + 1] != ' ') { //<==
                wordHyp[count] = '-';
                count += 1;
            } else if (word[i+3] && isVowel(word[i + 3]) && word[i + 2] != ' ') { //<==
                wordHyp[count] = word[i + 1];
                count += 1;
                wordHyp[count] = '-';
                count += 1;
                i++;
            } else if (word[i+4] && isVowel(word[i + 4]) && word[i + 3] != ' ') { //<==
                wordHyp[count] = word[i + 1];
                wordHyp[count + 1] = word[i + 2];
                wordHyp[count + 2] = '-';
                count += 3;
                i += 2;
            }
        }
    }
    printf("%s\n", wordHyp);
    return 0;
}