Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main(){
string sSentence;
ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,sSentence) )
    {
      cout << sSentence << '\n';
    }
    myfile.close();
  }
	else cout << "Unable to open file"; 

int iLen = sSentence.length();
int iBlank = sSentence.find(" ");

char cVowels[]={"AEIOUaeiou"};

	for(int o=0;0<iLen;o++){
			if(isalpha(sSentence[o]) && sSentence[o] == cVowels ){
					string sSen = sSentence.substr(o,iBlank);
					int iLength = sSen.length();
					int iVowel = sSen.find_first_of("aeiouAEIOU");
					string addSent = sSen.substr(0,iVowel);
					string newSent = sSen.insert(iLength,addSent);
					cout << newSent;
					cout<<"ay";
					o=iBlank-1;
				}
		
			else
		        cout << sSentence[o];
	} 
return 0;
}

i have this condition

C#
char cVowels[]={"AEIOUaeiou"};
if(isalpha(sSentence[o]) && sSentence[0] == cVowels )


the condition states that the character is a letter and it must be a vowel.
how can I compare the character to cVowels???

when I run the code it says:
"[error] expected primary - expression before ']' token
Posted
Comments
Kuthuparakkal 19-Feb-15 9:38am    
Add another loop to iterate cVowels and compare each character against sSentence[o]
PIEBALDconsult 19-Feb-15 9:46am    
How is that different from your post yesterday?
http://www.codeproject.com/Questions/877695/how-to-find-a-vowel-in-a-string-using-str-find?arn=0
Mohibur Rashid 19-Feb-15 20:40pm    
my suggested function would be:
bool isVowel(char c){
switch(c<'a'?c+' ':c){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
}
return false;
}

You are trying to compare an array to a single character: impossible. You need to search the array for each character, using something like find in the string class[^].
 
Share this answer
 
You'd need the == operator to work differently in order for this approach to work. (You can overload it) It would need to compare the single character to each of the characters in the array. In the interests of both maintainability and re-usability, you should really write a function that will take

(1) the character to check
(2) a null-terminated string that contains characters to check against

Returning true if (1) matches any of the chars in (2) - strtok employs an approach vaguely similar - that is, one of using a null-terminated string to pass what is really an array of otherwise-unrelated characters.

An other way you could do it would be to use a switch statement. Since multiple cases can have the same action, you could just string them all together in this naive manner, which would also actually be pretty fast, despite its awkward appearance.

C++
switch (sSentence[index])
{
	case 'a':
	case 'A':
	case 'e':
	case 'E':
	case 'i':
	case 'I':
	case 'o':
	case 'O':
	case 'u':
	case 'U':
                // character is a vowel
                // do something here
                break;

    default:
                // not a vowel
                // do something else here
}



EDIT: Added approach showing the use of a function.
C++
#include <cstdlib>
#include <cstdio>
#include <cstring>

bool isCharOneOf(char lookForMe, const char *inHere)
{
    while (*inHere)
    {
        if (lookForMe == *inHere)
            return true;
        inHere++;
    }
    return false;
}

int main(int argc, char *argv[])
{
    const char *searchString = "The quick brown fox jumps over the lazy dog";
    const char *vowels = "aeiouAEIOU";

    int nChars, curIndex, nVowels=0;
    nChars = strlen(searchString);
    for (curIndex=0; curIndex<nChars; curIndex++)
    {
        if (isCharOneOf(searchString[curIndex], vowels) == true)
            nVowels++;
    }
    printf("There are %d vowels in '%s'\n", nVowels, searchString);

    return 0;
}
 
Share this answer
 
v2
Comments
Reuben Cabrera 19-Feb-15 10:00am    
this is hard code :(
enhzflep 19-Feb-15 19:21pm    
Do you mean that this is hard-coded - i.e it's hard to change quickly, since important info is not parameterized? I figured we weren't going to change the ist of vowels any-time soon, so from a maintenance point of view it seemed okay. No good from a re-use perspective though.

Anyway, I've updated my solution with an example of using a function as I mentioned. Far cleaner and more useful than the approach that uses a switch. It just required marginally more thinking instead of marginally more typing.
You must compare sSentence[o] with every item of the cVowels array (or, at least, until a match is found), e.g.
change from
Quote:
if(isalpha(sSentence[o]) && sSentence[o] == cVowels )
to

C++
int n;
for (n=0; n<sizeof(cvowels); n++)  {
  if ( sSentence[o] == cVowels[n])
  { // OK, it is a vowel, do stuff here
   //..
   break;
  }
}
if ( n == sizeof(cvowels))
{ // no match: it is not a vowel
}
 
Share this answer
 
v3
i kinda found another different problem here.
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

bool FindVowel(char);

 bool FindVowel(char sWard){
 	string sword;
 	sword = sWard;
    int x =sword.find("AEIOUaeiou");
 	if(x==string::npos)
 	return false;
 	else
 	return true;
 }
 
 
int main(){
string sSentence;
ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,sSentence) )
    {
      cout << sSentence << '\n';
    }
    myfile.close();
  }
	else cout << "Unable to open file"; 

int iLen = sSentence.length();
int iBlank = sSentence.find(" ");



	for(int o=0;0<ilen;o++){>
			if(isalpha(sSentence[o]) && FindVowel(sSentence[o]) == true ){ 
					string sSen = sSentence.substr(o,iBlank);
					int iLength = sSen.length();
					int iVowel = sSen.find_first_of("aeiouAEIOU");
					string addSent = sSen.substr(o,iVowel);
					string newSent = sSen.insert(iLength,addSent);
					cout << newSent;
					cout<<"ay";
					o=iBlank-1;
				}
			else if(isalpha(sSentence[o])){
				continue;
			}
			else
		        cout << sSentence[o]; 
	} 
return 0;
} </fstream></string></iostream>



the line else if is suppose to determine if the character is a letter.
then if it is a letter its suppose to go to the loop. why is entering else statement??

the output prints (" ")
 
Share this answer
 
Comments
CPallini 19-Feb-15 11:48am    
It shouldn't. Are you sure sSentence[o] is actually alphabetic letter?
Please note you could have written just:
else if ( ! isalpha(sSentence[o])) cout << sSentence[o];
Reuben Cabrera 21-Feb-15 11:11am    
else if should print nothing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900