String Manipulation

Help needed desperately.

Page 1 of 1

12 Replies - 2995 Views - Last Post: 08 May 2007 - 09:15 AM Rate Topic: -----

#1 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

String Manipulation

Posted 05 May 2007 - 11:59 PM

Pig Latin is a commoner's tongue from the middle ages. It was developed to make the common man sound as fancy as his/her lord. It has nothing to do with pigs or Latin. [Or maybe Jason is just making all that up... But it is a 'real' language in use by millions every day. See the Wikipedia entry for more information -- just ignore that bit about programming it with recursion; you still aren't allowed to use recursion!]

To form a Pig Latin word from an English word, you first determine if the first letter is a vowel or consonant (vowels are a, e, i, o, u and sometimes y -- at least for the sake of this discussion). If it is a consonant, remove its consonant group (all consonants up to a vowel; but watch out for qu since it acts together as a consonant), move this group to the rear of the word, and add "ay" after it. If the first letter is a vowel, leave it there and simply append "yay" to the end of the word.

For example:

English Pig Latin English Pig Latin Commentary
is isyay a ayay vowel start
form ormfay to otay consonant start
them emthay the ethay consonant group start
through oughthray scroll ollscray [longer] consonant group start
qat atqay quad adquay q vs. qu start
yellow ellowyay sky yskay y start vs. y as vowel

Note in the bottom two examples how y is treated as a consonant at the beginning of a word but as a vowel inside/at the end of a word.

Here's a whole paragraph:

This is the winter of our discontent! My dog has fleas.
The rancid dogs of war move into our shallow graves. Happy
Gilmore has won the Penant!
It would become:

Isthay isyay ethay interway ofyay ouryay iscontentday! ymay
ogday ashay easflay. Ethay ancidray ogsday ofyay arway ovemay
intoyay ouryay allowshay avesgray. Appyhay Ilmoregay ashay
onway ethay Enantpay!
(Note how capitalization transfers to the new first letter. Note also how punctuation is not considered part of the word.)

Program Information
Write an English to Pig Latin translator. The user types in their sentence(s) (having a sentence end with a \n is acceptable) and you print them back out in Pig Latin.

Be sure to let the user repeat the entry/translation cycle as many times as they want.

Handing it in...
After you've placed your TPQ answers in a separate file named like your main program file, you'll need to run your script. Don't forget to execute your program as many times as you need to thoroughly test your program!




This is my big assignment.
How do I even start....
Could someone give me an example code for one of them?
so for consonant start...

if you find a word that starts with a consnant, add "that consonant +yay" at the end of the word and remove the first consonant character.

For this, how would i write a code for that?
PLEASE, HELP
THIS IS URGENT

Is This A Good Question/Topic? 0
  • +

Replies To: String Manipulation

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: String Manipulation

Posted 06 May 2007 - 08:04 AM

The site has a policy by which we prefer to see a good faith effort on the part of the user before providing source code for academic assignments. Can you post what you've written so far?

Generally speaking, you may wish to approach the consonant identification like this:

Check to see if the first letter is a vowel (each word in a separate string object). If so, append the first letter to the end, and add the required characters, The methods required are all available as part of the standard string class.

http://www.cpprefere...ring/index.html
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: String Manipulation

Posted 06 May 2007 - 08:19 AM

First of all relax... take a deep breath, ohmenypodme..........

The program is not very hard at all. There are a few tools that will make your job a little easier (not that you need them, but they make things easier).

What you will need is a tokenizer (splits a string up into "tokens" such as the words of a sentence -- for example a tokenizer might take "This is a test." and break it up into an array of strings, "This"|"is"|"a"|"test".

You may want to convert strings to lowercase for manipulation. (and then recapitalize the first letter).

once you have tokenized the string, now you need to check the first letter, and proceed according to the rules listed in your assignment.

None of this is hard, take it step by step. debugging as you go.

So question 1: Can you use the Standard Template Library (STL)?
Was This Post Helpful? 0
  • +
  • -

#4 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: String Manipulation

Posted 06 May 2007 - 09:57 AM

View PostNickDMax, on 6 May, 2007 - 08:19 AM, said:

First of all relax... take a deep breath, ohmenypodme..........

The program is not very hard at all. There are a few tools that will make your job a little easier (not that you need them, but they make things easier).

What you will need is a tokenizer (splits a string up into "tokens" such as the words of a sentence -- for example a tokenizer might take "This is a test." and break it up into an array of strings, "This"|"is"|"a"|"test".

You may want to convert strings to lowercase for manipulation. (and then recapitalize the first letter).

once you have tokenized the string, now you need to check the first letter, and proceed according to the rules listed in your assignment.

None of this is hard, take it step by step. debugging as you go.

So question 1: Can you use the Standard Template Library (STL)?


I am not sure if I can use the STL.
I will try to write something. I haven't written anything yet.
How do I separate words from a sentence?
This is something I need to know how to do before starting.
so something like

cout<<"Enter your sentence:"
cin>>sentence

and then
if .....
{
s.replace(0,


..something..
how do i separate words from a sentence???
Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: String Manipulation

Posted 06 May 2007 - 10:06 AM

There are a variety of ways, and your choice will depend on if you are going to use the STL or not...you'll have to clarify with you instructor.

Here are some common methods.

http://www.planet-so...p...26&lngWId=3

Check section 7.3 here:

http://www.linuxself...ng-HOWTO-7.html

A robust version:

http://www.codeproje...stringsplit.asp

A previous discussion:

http://www.dreaminco...h...70&hl=split

http://www.cse.iitb....pranavk/cpp.txt
Was This Post Helpful? 0
  • +
  • -

#6 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: String Manipulation

Posted 06 May 2007 - 10:21 AM

View PostAmadeus, on 6 May, 2007 - 10:06 AM, said:

There are a variety of ways, and your choice will depend on if you are going to use the STL or not...you'll have to clarify with you instructor.

Here are some common methods.

http://www.planet-so...p...26&lngWId=3

Check section 7.3 here:

http://www.linuxself...ng-HOWTO-7.html

A robust version:

http://www.codeproje...stringsplit.asp

A previous discussion:

http://www.dreaminco...h...70&hl=split

http://www.cse.iitb....pranavk/cpp.txt


Thank you for your provided information.
I will try to write a program tonight due to work.
I will post the code once I have it. I am sure it won't be good but I will try..
Could you please check it tonight and take a look at the code around midnight? so that I can show my teacher tomorrow ? well i need it at 11:00 am tomorrow so it won't be a problem if you can check tomorrow morning beore 11:00


Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#7 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: String Manipulation

Posted 06 May 2007 - 05:14 PM


#include<string>
#include<iostream>
#include<cctype>

using namespace std;

int main()
{
		string original, manipulated, newstring;

		original = "This is to test for a pig latin program.";
		manipulated = " ",newstring=" ";

		vector<string>words;
		words = split(original);

		for(unsigned int i=o; i<words.size(); i++)
		{
				if(i!=(words.size()-1))
				{
						manipulated=words[i].substr(1)+words [i].substr(0,1);
						words[i]=manipulated;
				}
				else
				{
						manipulated = words[i].substr(1,(words[i].length()-2))+
									  words[i].substr(0,1)+
									  words[i].substr((words[i].length()-1),1);
						words[i]=manipulated;
				}
		}

		for(i=0; i< words.size(); i++)
		{
				newstring += words[i];
				if (i!=(words.size()-1))
				{
						new string += " ";
				}
		}

		cout<<newstring<<endl;
		return 0;
}




Here it is
Now where should i insert all the codes for pig latin words..
if first character= const, blahblah
is there something i can use for "move the first char to the last and add "yay""
what will be the code for that?

and above code splits a long sentence into words.. compiler says not successfully coded though..
Please help!!!
thanks

Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: String Manipulation

Posted 06 May 2007 - 07:06 PM

What is the exact error message the compiler is giving?
Was This Post Helpful? 0
  • +
  • -

#9 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: String Manipulation

Posted 06 May 2007 - 09:23 PM

View PostAmadeus, on 6 May, 2007 - 07:06 PM, said:

What is the exact error message the compiler is giving?



pig.C: In function `int main()':
pig.C:14: error: `vector' was not declared in this scope
pig.C:14: error: expected primary-expression before '>' token
pig.C:14: error: `words' was not declared in this scope
pig.C:15: error: `split' was not declared in this scope
pig.C:17: error: `o' was not declared in this scope
pig.C:17: warning: unused variable 'o'
pig.C:33: error: name lookup of `i' changed for new ISO `for' scoping
pig.C:17: error: using obsolete binding at `i'
pig.C:38: error: invalid operands of types `std::string*' and `const
char[2]' to binary `operator+'
pig.C:38: error: in evaluation of `operator+=(struct std::string*,
const char[2])'
pig.C:14: warning: unused variable 'vector'
pig.C:15: warning: unused variable 'split'


this


and give you please give me an example for one of the codes i need to write for changing the word?
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: String Manipulation

Posted 07 May 2007 - 05:13 AM

Well, may of your errors are caused because you have not included the proper libraries. For vector use, you must include the vector header.
#include<vector>


another few errors are being caused because you have copied and pastes the code from one of the links I provided without actually reading the post. If you return to this link

http://www.dreaminco...h...70&hl=split

and take a moment to read what it has to say, you'll note that it uses a split function defined elsewhere (the link to it is also in that post). Although I'm sure you are aware, you cannot simply copy and paste code without understanding the context and expect it to work immediately. Take a moment to read the notes provided.

Another error is being caused by the following:
for(unsigned int i=o;


I'm assuming you wanted to assign the numerical value of 0 to the variable i, not the letter o.

Once you have those corrected, we should be able to more effectively debug.
Was This Post Helpful? 0
  • +
  • -

#11 ArunBhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-May 07

Re: String Manipulation

Posted 07 May 2007 - 05:57 AM

This code might work, but there is proble with my STL library
pig=temp2;
pig+=temp;
cout<<pig<<endl;
pig+=yay;
cout<<pig<<endl;

Check it might work on ur machine.
#include <iostream>
#include <string>


using namespace std;

string ovels = "AaEeIiOoUu";
string ay="ay";
string yay="yay";
 
void getWord(char* fullString,string& word,int& pos)
{
	int count = -1;
	word = fullString[pos++];
	while(fullString[pos]!=' ')
	{
		word+=fullString[pos];
		pos++;
	}
	word+='\0';
}
void ConverttoPig(string word)
{
	string temp;
	string pig,temp2;

	bool flag=false;
	for(int i=0;i<word.size();i++)
	{
		char pos=100;
		char ch = word[i];
		pos = ovels.find(ch,0);
		if(pos < ovels.size() && i==0)		
		{
			flag=true;
			word+=ay;
			return;
		}
		if(pos< ovels.size())
		{
				temp2 ="";
				temp2 = word.substr(i,word.size());
				pig=temp2;
				pig+=temp;
				cout<<pig<<endl;
				pig+=yay;
				cout<<pig<<endl;
				return;
		}
		temp+=word[i];
	}
	word=pig;
}
int main(int argc, char* argv[])
{
	
	char str[100]="Hrere is a code snippet",modStr[100];
	string modified="";
	//cout<<"Enter the Sentence:\n";
	//cin<<str;
	int pos=0;
	while(str[pos]!='\0')
	{
		string wordd;
		getWord(str,wordd,pos);
		ConverttoPig(wordd);
		modified+=wordd;
		pos++;
	}

	cout<<modified;
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#12 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: String Manipulation

Posted 07 May 2007 - 09:43 AM

#include<string>
#include<iostream>
#include<cctype>
#include<vector>

using namespace std;

vector<string> split(const string & s, const string & sep = " ");


int main()
{
		string original;
		vector<string> words;

		original = "This is to test for a pig latin program.";

		words = split(original);

		for(vector<string>::size_type i=0; i != words.size(); i++)
		{
			 // call pig translator for each words[i]
		}
		 // display all the words

		return 0;
}

// function to translate a single word to pig latin
	// case for vowels:
	// watch for leading y's since they are consonants!
	// case for consonants:
	// watch for 'qu' at beginning!

/*
   .append
   +=
*/

//

vector<string> split(const string & s, const string & sep)
{
		vector<string> ret;
		string::size_type i=0, j=0;
		while (i!=string::npos)
		{
				i = s.find_first_not_of(sep,j);
				j = s.find_first_of(sep, i);
				if ( i!=string::npos && i!=j)
				{
						ret.push_back(s.substr(i,(j-i)));
				}
		}
		return ret;
}




This is what I have so far. I edited it and now the code itself is good.
now i need to add a function to translate the words...
Can someone give me an example for consanats as mentioned above?

so """ Find a word that starts with a consonant and append it to the end and add "ay" after the the consonant"""
How do I code this ? I think if i see an example i will be able to do the rest of the remaining translation..?...
Help!Please
Was This Post Helpful? 0
  • +
  • -

#13 eugene1017  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 22-April 07

Re: String Manipulation

Posted 08 May 2007 - 09:15 AM

View Posteugene1017, on 7 May, 2007 - 09:43 AM, said:

#include<string>
#include<iostream>
#include<cctype>
#include<vector>

using namespace std;

vector<string> split(const string & s, const string & sep = " ");


int main()
{
		string original;
		vector<string> words;

		original = "This is to test for a pig latin program.";

		words = split(original);

		for(vector<string>::size_type i=0; i != words.size(); i++)
		{
			 // call pig translator for each words[i]
		}
		 // display all the words

		return 0;
}

// function to translate a single word to pig latin
	// case for vowels:
	// watch for leading y's since they are consonants!
	// case for consonants:
	// watch for 'qu' at beginning!

/*
   .append
   +=
*/

//

vector<string> split(const string & s, const string & sep)
{
		vector<string> ret;
		string::size_type i=0, j=0;
		while (i!=string::npos)
		{
				i = s.find_first_not_of(sep,j);
				j = s.find_first_of(sep, i);
				if ( i!=string::npos && i!=j)
				{
						ret.push_back(s.substr(i,(j-i)));
				}
		}
		return ret;
}




This is what I have so far. I edited it and now the code itself is good.
now i need to add a function to translate the words...
Can someone give me an example for consanats as mentioned above?

so """ Find a word that starts with a consonant and append it to the end and add "ay" after the the consonant"""
How do I code this ? I think if i see an example i will be able to do the rest of the remaining translation..?...
Help!Please


Could somebody help?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1