gpt4 book ai didi

c - 如何在数组中查找辅音并在c中在它们后面附加ut

转载 作者:行者123 更新时间:2023-11-30 21:01:06 25 4
gpt4 key购买 nike

我正在开展一个项目,需要一些帮助。我在作业中被告知以下内容:

The Language of Tut

The tut language is a simple encoded language where words are spelled out and the letters "ut" are placed after every consonant, vowels are left as they are. So the word "Hello", in tut become "Hutelutluto" The word "Goodbye" becomes "Gutoodutbutyute". The sentence "How are you today?", becomes "Hutowut arute yutou tutodutayut?".

The Programs

For this lab you will need to write two (2) C programs. The first should be be able to accept an English sentence from the user and convert it into Tut and the second program should accept a Tut sentence from the user and convert it to English. Both the original and converted sentences should be displayed for the user in both programs.

Functions and Header Files

Find at least one reason to create a function that can be used in both of the two programs. The function should have high functional integrity and be loosely coupled. The function should be kept in a header file that will be included into each of the two programs.

You should submit two C source code files and 1 header file for this assignment. Everything that's needed for the programs to compile must be included.

我现在不关心这个功能。只是试图用数组完成主要任务。

这是我到目前为止所拥有的。

/*
EnglishToTut
*/


#include <stdio.h>

int input_check (char [], char [], int);

int main ( void ) {

const max_str_len = 50;
int c, i;
char eng_str[max_str_len];
char vowel[] = {'a','e','i','o','u','A','E','I','O','U'};
char consonant[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'y', 'z', '\0'};

printf("Welcome to the program Tut.\n");
printf("Please enter a string to convert to Tut: ");
scanf(" %s", eng_str);

for ( c = 0; c < max_str_len; c++) {
if (eng_str[c] == consonant[c]) {
printf("%sut", eng_str);
}
}

// while ( input_check(consonant, eng_str, max_str_len) != 1 ) { //write a loop with an if statement that uses function to check for consanants and then append ut.
// if( input_check(consonant, eng_str, max_str_len) == 1 ) {
// printf("%s\n", eng_str);
// } else if ( input_check(consonant, eng_str, max_str_len) == 0 ) {
// printf("%s\n", eng_str);
// }
// }

请忽略注释代码。但我的问题是为什么程序只将 ut 附加到字符串的第一个字符而不是每个辅音?

最佳答案

您的问题是您正在循环索引到具有相同索引的第一个和第二个数组。尽管您正在循环遍历字符列表并在特定条件语句下将“ut”附加到字符,但仅当两个数组索引处的字符相同时才执行此函数。在 if 语句周围添加一个辅助循环,并使用此循环来索引辅音数组。为了使您的程序更加简洁,删除辅音数组并仅检查该字符是否不是元音可能会更容易。试试这个...

#include <stdio.h>

int inArr(char character, char *letters, int size) {//retuns the number of times a character appears in an array
int counter=0;
for(int i=0; i<size; i++) {
if(character==letters[i]) {
counter++;
}
}
return counter;
}

int main () {

int maxLen = 50;
char eng_str[maxLen];
char vowels[] = {'a','e','i','o','u','A','E','I','O','U', ' ', 0};//array of characters withought "ut" appended

printf("Welcome to the program Tut.\n");
printf("Please enter a string to convert to Tut: ");

int i=0;//gets string and saves to eng_str
while((eng_str[i] = getchar())!='\n') {
i++ ;
if(i>=maxLen) {
printf("\nSTRING TOO LONG\n");
return 1;
}
}
int a=0;//stores the length of the typed message as a
while(eng_str[a] != '\n') {
a++;
}

for(int i=0; i<a; i++) {//loop through entered string
if(inArr(eng_str[i], vowels, (sizeof(vowels)/sizeof(vowels[0])))==0) {//if character at index i is not in vowels append ut
printf("%cut", eng_str[i]);
}else {//else print just the character
printf("%c", eng_str[i]);
}
}
printf("\n");
}

我使用了 getchar() 而不是 scanf(),因为很难根据 scanf()< 的输入来更改数组的长度 并使用 getchar() 您可以告诉它搜索 '/n' 字符。 scanf() 还将字符串截断为字符串中的第一个空格。

您还可以尝试将字符动态分配给字符数组,但如果您声明 char *eng_str 然后尝试将字符传递给它,程序会抛出段错误。

关于c - 如何在数组中查找辅音并在c中在它们后面附加ut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36961404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com