#include <iostream>
#include <string>
/*pig latin is supposed to take each word saparately
and delete the first character and add it to end
of the word then add "AY"
Example:
peak ==> eakpay
*/
usingnamespace std;
int main()
{
string str1, str2;
getline(cin, str1);
while ((str1.length())>=1)
{
str2.substr(0, (str1.find(" ")-1) );
str1.erase(0, str1.find(" ") );
str2=str2+str2.at(0);
str2.erase(0);
str2=str2+"AY";
cout << " " <<str2;
str2.clear();
}
cout << endl;
system("pause");
return 0;
}
This took me a while to figure out. I tried to explain the code through my comments. I figure out how to do it for one input then brought that over to how to do it for many inputs.
#include <iostream>
#include <string>
/*pig latin is supposed to take each word saparately
and delete the first character and add it to end
of the word then add "AY"
Example:
peak ==> eakpay
*/
usingnamespace std;
int main()
{
string str1, str2;
getline(cin, str1);
if (str1.length()>=1)
{
str2 = str1.substr(0,1) + "ay";
str1.erase(0,1);
str1 = str1 + str2;
cout << str1;
}
cout << endl;
return 0;
}
#include <iostream>
#include <string>
/*pig latin is supposed to take each word saparately
and delete the first character and add it to end
of the word then add "AY"
Example:
peak ==> eakpay
*/
usingnamespace std;
int main()
{
int arraySize = 3; //array size is the number of strings you want the user to put in.
string words[arraySize ]; //stores user iputed array.
string str2;
//gets the input from the user. stores input in each array index.
for(int i = 0; i < arraySize ; i++)
cin >> words[i];
//prints the pig latin translation.
for( int i = 0; i < arraySize ; i++)
{
if(words[i] != " ") //make sure the user put in a string.
{
str2 = words[i].substr(0,1) + "ay"; //get the first letter of the input string and add it to "ay".
words[i] .erase(0,1); //remove first letter in string.
words[i] = words[i] + str2; //stick it together.
cout << words[i] << " "; //final output.
}
}
}
Have you any restrictions on what standard classes and functions you can use?
To start with, it looks like you should read up about the calls your are trying to use. For example:
str2.substr(0, (str1.find(" ")-1) );
substr returns a substring, but I don't see you storing the return result anywhere. And you read the user's input into str1, not str2, so str2 is empty to start with.
Also, your loop termination condition and substr usage are all a bit off. And why are you repeating the str1.find call rather than storing the result and then reusing it?
To work out what you code is doing so you can fix it, using the current approach, add cout statements to write out the contents of the strings.
It should be easy enough to fix when you see what's going on, with the help of a C++ reference. For example, erase call on line 21 needs to be given a length as well as a start index. The default length (to erase) is the whole string!
Andy
PS Once you get your first version to work, then you can rework it to avoid the erase calls. If you track the start and end of words, it's easy enough to do. (Then code can work on const strings as well as non-const ones.) Or you could use an istringstream.
Thanks a lot hertz and andy I clearly see now the problem.
I am new to string and this goddamned program was an olympiad question and I knew nothing about strings
P.S : I have another project for you. With the code that you have recieved, see if you can have a program that takes in a pig latin code and translates it
to regular text.
Here It is my repaired code.
But I got a small problem If you know how to answer my question
LOOK THE SOURCE CODE!!!
THE COMMENT AT LINE 20 IS MY QUESTION...
#include <iostream>
#include <string>
/*pig latin is supposed to take each word saparately
and delete the first character and add it to end
of the word then add "AY"
Example:
peak ==> eakpay
*/
usingnamespace std;
int main()
{
string str1, str2;
int a;
getline(cin, str1);
while (a>=0)
{
a=str1.find(" ");
str2 =str1.substr(0,a);
str1.erase(0,a+1); //I dont know why when I erase from 0 to a it doesnt erase the space
str2=str2+str2.at(0);
str2.erase(0,1); //Here instead it erases the first
str2=str2+"AY";
str2.clear();
}
cout << endl;
system("pause");
return 0;
}
substr takes a beginning index (0) and the number of characters to copy (4) which means on line 19 you end up with str2 equal to "peak".
erase takes a beginning index (0) and the number of characters to erase (4) which means it behaves just like substr with regards to the elements of the string it directly affects.
Why, then, are you surprised at the behavior of erase but not the behavior of substr?
cire I was surprised even by that but now I think I understand so the second number ("a") stands for number of characters including 0-th character... Thanks a lot!!!
Andy try to compile it in Dev C++ or CodeBlock (I dont like Visual C++) It gives me the same error as yours... And about the loop I guess it is OK because a is the place where space is found and when there is no space that means it is the end of the string so the loop will break then...
Switching compilers won't remove the bugs; it just means the compiler is a bit dopey (are you compiling with warnings enabled??)
When string::find doesn't find anything, it returns string::npos, which is a big positive number. In VC++'s case, it's actually 0xFFFFFFFF, so when you add 1 it wraps and you end up with 0. This means the erase on line 20 ends up doing nothing (it's asked to delete 0 elements). So the last part of the string is never removed, and the loop goes on forever.
It is better, if you're sticking with erase, to use:
#include <iostream>
#include <string>
/*pig latin is supposed to take each word saparately
and delete the first character and add it to end
of the word then add "AY"
Example:
peak ==> eakpay
*/
usingnamespace std;
int main()
{
string str1, str2;
size_t a = 0;
getline(cin, str1);
while (!str1.empty()) // Edit : where there's more string (it's not empty)
{
a=str1.find(" ");
str2 =str1.substr(0,a);
if(a == str1.npos) // if there's no other space
str1.clear(); // delete the rest of the string
else
str1.erase(0,a+1); // or delete up to the space
str2=str2+str2.at(0);
str2.erase(0,1);
str2=str2+"AY";
cout << " " <<str2; // put output back
str2.clear();
}
cout << endl;
system("pause");
return 0;
}
But the code is still not quite right, in that it won't gracefully handle extra embedded or trailing spaces. Etc.