Hi.

I have to write a program that will return the amount of all the vowels on a string.. I already did it, but it freezez during the execution :sad: . Any suggetions are welcome.

ps.I must say that I am a begginer on this... :o

Here is the code:

#include <iostream.h>
#include <string.h>
#include <iomanip.h>


void cuenta_vocales(char palabra[])
{
	char vocales[6],
		 *pch;
	int count=0;
	

	vocales[0]= 'a';
	vocales[1]= 'e';
	vocales[2]= 'i';
	vocales[3]= 'o';
	vocales[4]= 'u'; 

	for (int i=0; i<=4; i++)
	{
		strcpy (pch,palabra);

		while( pch != NULL )
		{
			pch=strchr(pch, vocales[i]);
			if( pch!=NULL ) 
			{
			count++;
			}
		}

	} 
	cout<<"Cantidad de vocales en "<<palabra<<": "<<count<<endl;

}

void main()
{
	char  palabra[30];
	cout<<"Entre la palabra que desea evaluar:\n";
	cin>>palabra;
	cuenta_vocales(palabra);
}

<< moderator edit: added [code][/code] tags >>

Recommended Answers

All 3 Replies

You've pretty much checked off everything on the "don't do" list of basic C. Maybe you should start using the standard C++ classes instead of fumbling around with the low level arrays and pointers of the C subset of C++. It's easier to get right if you don't have years of experience with the low level stuff:

#include <algorithm>
#include <iostream>
#include <string>

bool is_vowel(std::string::size_type c)
{
	std::string vocales("aeiou");
	return vocales.find(c) != std::string::npos;
}

void cuenta_vocales(std::string palabra)
{
	int count = count_if(palabra.begin(), palabra.end(), is_vowel);
	std::cout<<"Cantidad de vocales en "<<palabra<<": "<<count<<std::endl;
}

int main()
{
	std::string palabra;
	std::cout<<"Entre la palabra que desea evaluar: ";
	std::cin>>palabra;
	cuenta_vocales(palabra);
}

Hi.

I have to write a program that will return the amount of all the vowels on a string.. I already did it, but it freezez during the execution :sad: . Any suggetions are welcome.

ps.I must say that I am a begginer on this... :o

Here is the code:

#include <iostream.h>
#include <string.h>
#include <iomanip.h>


void cuenta_vocales(char palabra[])
{
	char vocales[6],
		 *pch;
	int count=0;
	

	vocales[0]= 'a';
	vocales[1]= 'e';
	vocales[2]= 'i';
	vocales[3]= 'o';
	vocales[4]= 'u'; 

	for (int i=0; i<=4; i++)
	{
		strcpy (pch,palabra);

		while( pch != NULL )
		{
			pch=strchr(pch, vocales[i]);
			if( pch!=NULL ) 
			{
			count++;
			}
		}

	} 
	cout<<"Cantidad de vocales en "<<palabra<<": "<<count<<endl;

}

void main()
{
	char  palabra[30];
	cout<<"Entre la palabra que desea evaluar:\n";
	cin>>palabra;
	cuenta_vocales(palabra);
}

<< moderator edit: added [code][/code] tags >>

Ok i finally did it! here is a piece of the code....

while (position!=string::npos)
{
count++;
position = word.find ("a", position+1);
}
thanks for the reply!

Viva la std::string!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.