Thread: Help with switcharoo program.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Help with switcharoo program.

    So I am writing this program that takes in two strings from console and then switches the vowels in string1 for the vowels in string two in order of ocurrance. For example:

    text 1 = rapid
    text 2 = ouch
    result string = ropud

    this is what i have so far:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <cstring>
    
    
    using namespace std;
    
    
    bool isVowel (char);
    
    void main()
    {
    	string text1, text2, translation;
    	cin >> text1;
    	cin >> text2;
    
    	cout << "You entered: " << text1 << endl;
    	cout << "You entered: " << text2 << endl;
    
    
    
    	string result;
    	int i1 = 0;
    	int i2 = 0;
    
    	//while ( i1 < s1Length() && i < s2Length())
    	while ( i1 < text1.length() || i1 < text2.length())
    	{
    		for (;i1 < text1.length() && isVowel(text1[i1]) ; i1++)
    		{
    			if(i1 < text1.length())
    				result += text1[i1];
    		}
    		cout << result;
    	}
    }
    
    bool isVowel (char c)
    {
    	const string vowels = "aeiouAEIOU";
    	for (int i = 0; i < vowels.length(); i++)
    		if (c == vowels[i]){
    			return true;}
    
    		return false;
    }
    but I think there has got to be a better way of doing this. As of now, I am not sure what my next step should be or if the program is ok. Can anyone suggest anything or point out something I did wrong?

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Main ALWAYS returns an int. Use int main() or int main ( void ).
    Double Helix STL

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1. Are the two strings guaranteed to have the same number of vowels? Or text2 has at least as many as text1.

    #2. main returns int, not void.

    #3.
    Code:
    int i1 = 0;
    int i2 = 0;
    Those are better as:
    Code:
    string::size_type i1 = 0;
    string::size_type i2 = 0;
    My pseudocode would be:
    • Read text1 and text2 from user, prompt them.
    • Copy text1 into result string
    • while( can find a vowel in result string )
      • Get next vowel character from text2
      • Set char in result string in same position as that returned from above "while" loop to char
        returned from above line

    I'd personally just use the string class's find_first_of functions to find a vowel. The above while part could look like:
    Code:
    string::size_type i1 = 0;
    while( (i1 = result.find_first_of("aeiouAEIOU",i1)) != string::npos )
    {
        ++i1;
        ...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    No, they are not guaranteed to have the same number of vowels. In this case, there is something else I have to do. For now, is there a way to make it work using some variation of the code i posted above?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <cstring>
    
    
    using namespace std;
    
    
    bool isVowel (char);
    
    int main()
    {
    	string text1;
    	string text2;
    	string result;
    	cin >> text1;
    	cout << text1 << endl;
    	cin >> text2;
    	cout << text2 << endl;
    
    	for (int i =0; i < text1.length(); i++)
    	{
    		text1[i] = result[i];
    	}
    	for (int k =0; k < text2.length(); k++)
    	{
    		if (isVowel(text2[k]))
    		{
    			char b = text2[k];
    			cout << b;
    		}
    		
    		
    	}
    	
    }
    
    bool isVowel (char c)
    {
    	const string vowels = "aeiouAEIOU";
    	for (int i = 0; i < vowels.length(); i++)
    		if (c == vowels[i]){
    			return true;}
    
    		return false;
    }
    
    hk, is this the right track?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    In pseudocode:

    Code:
    'v' = first vowel in string 2
    For each letter 'x' of string 1:
        If 'x' is a vowel:
            Append 'v' to output
            'v' = next vowel in string 2
        Else:
            Append 'x' to output

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Ok, I came up with this code but don't how to code that once the first vowel from 2 has been used, the program should seek a second vowel...
    Code:
    // vowel - Lab.cpp : Defines the entry point for the console application.
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <cstring>
    
    
    using namespace std;
    
    
    bool isVowel (char);
    
    int main()
    {
    	string text1;
    	string text2;
    	string result;
    	cin >> text1;
    	cout << text1 << endl;
    	cin >> text2;
    	cout << text2 << endl;
    
    	for (int i =0; i < text1.length(); i++)
    	{
    		if(isVowel(text1[i]))
    		{
    			for (int k = 0; k < text2.length(); k++)
    			if (isVowel(text2[k]))
    			{
    				char b = text2[k];
    				text1[i] = b;
    				cout << text1 << endl;
    				
    			}
    		}
    
    	}
    	
    		
    }
    
    bool isVowel (char c)
    {
    	const string vowels = "aeiouAEIOU";
    	for (int i = 0; i < vowels.length(); i++)
    		if (c == vowels[i]){
    			return true;}
    
    		return false;
    }
    any suggestions?

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One way would be to make a string of all the vowels in text2. So you wouldn't have to search for them if you need to use them more than once (text1 has more vowels to replace).

    Then replace the first found vowel in text1 with the first extracted vowel, second with second etc. If you run out of vowels in this list, start over from 0.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    anon, I am not sure how to accomplish that at the moment. can you give me an example and I will work from there.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Fredir View Post
    anon, I am not sure how to accomplish that at the moment. can you give me an example and I will work from there.
    Whether you should do what anon says depends on what your assignment says. What ARE you supposed to do when the number of values in string1 is more than the number of vowels in string2?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM