编写一个C程序,从字符串中删除所有元音? [英] Write a C program that removes all vowels from a string?

查看:212
本文介绍了编写一个C程序,从字符串中删除所有元音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在c中编写一个函数来删除给定字符串中的所有元音,然后将所有字母移到左侧,以便在删除元音时有空位。这个函数应该如何工作的一个例子是,如果字符串输入是'hello',程序应该返回'hll'(元音没有空的空格)

注意这是一个函数所以函数定义应该是:

void removeVowels(char * word)

非常感谢你的帮助。谢谢:)



我尝试了什么:



这就是我的意思已经完成但错了:

  void  removeVowels( char  *字)
{
char 字[ 100 ], newString [ 100 ];
int i,j = 0 ;
for (i = 0 ; i< = strlen(word); i ++){
if (word [i] == ' a' || word [i] == ' e' || word [ i] == ' i' || word [i] == ' o' || word [i] == ' u'){
word [i] = ' ' ;
}
else {
newString [j ++] = word [i];
newString [j] = ' \ 0';
}

解决方案

您的代码不起作用的原因之一是因为您使用作为参数和局部变量,这是永远不会做的。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

I need to write a function in c to remove all vowels from a given string and then move all letters to the left so that there are o empty places when a vowel is removed. an example of how this function should work is that if the string input is 'hello', the program should return 'hll'(with no empty spaces where the vowels were)
note that this is a function so the function defintion should be:
void removeVowels(char* word)
your help is much appreciated. thanks :)

What I have tried:

thats what i have done but its wrong:

void removeVowels(char* word)
{
	char word[100],newString[100];
	int i, j = 0;
	for (i = 0; i <= strlen(word); i++) {
		if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u') {
			word[i] = ' ';
		}
		else {
			newString[j++] = word[i];
			newString[j] = '\0';
	}

解决方案

One of the reasons your code don't work is because you use word as a parameter and as local variable, which is a never do.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.


这篇关于编写一个C程序,从字符串中删除所有元音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆