7 Replies - 457 Views - Last Post: 05 May 2013 - 03:26 PM Rate Topic: -----

#1 me-robot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-May 13

Help me fix this program

Posted 03 May 2013 - 06:52 PM

Q: Starting with a character array with the letters in the word Antidisestablishmentarianism, create a vector of strings whose first element is Antidisestablishmentarianism, the second is Antidisestablishmentariani, the third is Antidisestablishmentaria, etc. until the last vector contains only an A. The trend is the next element in the vector is the previous element with the letters from the end removed until it runs into a vowel. Then it continues doing this until it reaches the start of the word (that is why the last element is just an A). Your program should work no matter given any character array.

** How am I manupulate the trend of the this question.

My Code:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
void printVector( vector<string> v )
{
for(unsigned int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
return;
}
int main()
{
string str=" ";
 
vector<string> myVector;
char cPhrase[] = {'A','n','t','i','d','i','s','e','s','t','a','b','l','i','s','h','m','e','n','t','a','r','i','a','n','i','s','m','\0'};
 
int limit = sizeof(cPhrase);
int j = limit;
 
for(int x = 0; x < limit; x++)
{
str = str+cPhrase[x];
}
 
for(int i=0; i < limit*2-2; i=i+2)
{
myVector.push_back(str.substr(0 ,j ));
 
printVector(myVector);
 
myVector.clear();
j=j-2;
myVector.push_back(str.substr(0 ,j ));
printVector(myVector);
 
myVector.clear();
j=j+1;
 
}
cout << endl;
return 1;
}

This post has been edited by jimblumberg: 03 May 2013 - 07:34 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Help me fix this program

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Help me fix this program

Posted 03 May 2013 - 08:56 PM

I notice you've included the <string> header, is there any reason you can't use a string instead of the character array?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 me-robot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-May 13

Re: Help me fix this program

Posted 04 May 2013 - 09:49 AM

Jim,

Thanks for the reply. I just used the character array because I found it easy and my teacher was more focused on it when I took C++ class.
Can you please help me with VOWEL trend that my program suppose to follow.

Thanks,
Sara
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Help me fix this program

Posted 04 May 2013 - 10:14 AM

Are you willing to use a std::sting instead of the C-string?

If so you may want to look into the find_last_not_of() member function, the erase member function.

Otherwise you'll need to iterate thru your C-string from the end when you find a vowel insert the end of string character and copy the string to your vector.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 me-robot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-May 13

Re: Help me fix this program

Posted 04 May 2013 - 10:29 AM

Jim,

Can you demonstrate the c-string idea, please.

Thanks,
Sara
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Help me fix this program

Posted 04 May 2013 - 10:36 AM

No I won't give you the code. Do you know how to iterate backwards trough your string? Try it and print the contents backwards and see what you can figure out.



Jim
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Help me fix this program

Posted 04 May 2013 - 03:15 PM

Hi,

What you need is a function that returns true when it encounters a vowel

and false when it is not a vowel

a function definition like ths would suffice

bool IsVowel(char StringElement);

then you need to loop backwards through your string str

calling your function IsVowel(str[element])

if true have a second string, lets call it result

and have that equal the str string up to that point something like.

        for(unsigned int u =i; u>0; u--)
	{
		
		result[u]= str[u];
	    
	}



where i is the decreasing length of str.

and then push back result into myVector.

That will then give you the entire solution to the problem

at the end just use your printVector function.

Best Wishes

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#8 me-robot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-May 13

Re: Help me fix this program

Posted 05 May 2013 - 03:26 PM

Thanks Snoopy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1