+ 1
Does s.size() mean the length of string s?
In Pig Latin, I try to make a loop with s.size(). However, when I check s.size(), it only returns the length of a word. Do you know how to fix this, and what do I need to use for knowing the length of a string? Thank you very much.
8 Respuestas
+ 1
Can you show your try..?
+ 1
Well, it is just a kind of a simple loop like this:
for (int i = 0; i < s.size(); i++) {
t=t+s[i];
}
For example, the input is "Hello World", but it only takes the "Hello" part and ignore the "World" part.
+ 1
Manav Roy Can you tell me more details about the getline function?
+ 1
Both the size() and length() methods return the length of a string - This is in terms of bytes. A char is one byte in memory. So as long as you are using characters which are stored in one byte, you’ll be fine. For the spaces, you can use the getline method:
std::string str;
std::getline(std::cin, str);
This will store the user input in str and keep the spaces.
0
How are you reading input?
pls Post full code ? May be you are using cin>>s; Huy082125
0
Huy082125 Is there a built-in function for that? It would make a lot of lives easier
0
Huy082125 it's better to show your full code if you not solved it yet.. That may help to find mistake.
As already posted by above one's, To read a line of string, use
string str;
getline(cin, str);
0
Manav Roy thank you, I really really needed this