c – 删除字符串中的元音.我究竟做错了什么?
发布时间:2020-12-16 09:57:58 所属栏目:百科 来源:网络整理
导读:我试图通过将所有辅音放入不同的数组然后将第一个数组重新初始化为第二个数组来删除字符串的所有元音.之后,应该打印第一个数组,即只有辅音.我真的不确定问题出在哪里,我一直在看这个问题好三个小时,我已经厌倦了. #include stdio.h#include stdlib.h#include
我试图通过将所有辅音放入不同的数组然后将第一个数组重新初始化为第二个数组来删除字符串的所有元音.之后,应该打印第一个数组,即只有辅音.我真的不确定问题出在哪里,我一直在看这个问题好三个小时,我已经厌倦了.
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char str [100]; char garbage[100]; int loopcondition = 0,j = 0,k = 0; int userinput; char a = "a",e = "e",i = "i",o = "o",u = "u"; printf("!!!Hello World!!! Please type in a string from which I can remove all the vowels:n"); //This program is supposed to take in a string and then delete all the vowels gets(str); //gets user input userinput = strlen(str); //makes userinput equal to the length of the string while(loopcondition < userinput) //the loop runs until the condition is no longer smaller than the input { loopcondition++; //incrementation of condition if(str[j] != a || e || i || o || u) // this should check wether the current position in the array is a vowel { garbage[k] = str[j] ; //if it's not the it puts the current character into the garbage array k++; //iteration of the garbage array position }; j++; //incrementation of string array position }; garbage[k] = ' '; strcpy (str,garbage ); //this copies the string?!?! aka reinitiliazing array variable printf("%s",str); return 0; } 解决方法
您的char初始化应该是这样的
char a = 'a',e = 'e',i = 'i',o = 'o',u = 'u'; 请记住,双引号(“”)用于字符串文字,单引号(”)用于字符文字. 然后, if(str[j] != a || e || i || o || u) 这不是你在c中使用逻辑OR(||)运算符的方式.链接是不可能的.你必须分别检查每个条件.就像是 if( (str[j] != a) && (str[j] != e) && (str[j] != i).......//so on 但是,在我看来,如果你改变逻辑以使用开关盒,它将是一个更好的设计. 哦,最好使用int main(void),这在标准中是推荐的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |