0

I am trying this following code in cpp

#include <iostream>
#include <string>
void Strlength(const string& s) {
    cout << s.length() << endl;
}
int main() {
    string s;
    cin >> s;
    cout << s.length() << endl;
    Strlength(s);
    return 0;
}

The string which I am giving as input is 100,000 characters long and is like "xxxxxxx...xxxxabcde"(fill the ... with remaining x)

This gives me the output as

4095
4095

I am expecting the output to be 100000. what am I doing wrong?

This relates to one of the hackerrank problem (Test case 10): String Similarity

RC0993
  • 800
  • 5
  • 28
  • @DimChtz I have tried replacing `s.length()` with `s.size()`. But still it shows `4095` – RC0993 Jun 15 '19 at 14:10
  • @πάνταῥεῖ What is missing? I have provided all the needed details. I dont thin `#include` and `using namespace std;` should be considered as missing info... Plus I have given more info (which is just extra TBH) by putting hackerrank link. – RC0993 Jun 15 '19 at 14:12
  • What compiler are you using? The code seems to work fine for me: `python -c 'print("x"*100000)' | ./a.out 100000 100000` – Stephen Newell Jun 15 '19 at 14:12
  • I am using g++. – RC0993 Jun 15 '19 at 14:13
  • Where did the massive string come from? `cin`? `argv`? A really big string literal? Are you on Windows? – Retired Ninja Jun 15 '19 at 14:14
  • @RetiredNinja it comes from `cin`, I am on linux. Iam just pasting those 100000 character string copied from a website (hackerrank) on the terminal window, when my program reaches `cin` and waits for the input – RC0993 Jun 15 '19 at 14:14
  • 2
    @RC0993 Maybe show us a compiliable example where you 1st write those 100000 characters to a file, and read that file into a single string in a second step. If you can reproduce your results with this, your question might be valid. – πάντα ῥεῖ Jun 15 '19 at 14:15
  • You could also try using my command line above, since that'll pass 100,000 characters to stdin. – Stephen Newell Jun 15 '19 at 14:19
  • 1
    Chances are you're hitting some limit, especially since you're getting 4k-1 characters which doesn't seem coincidental. – Retired Ninja Jun 15 '19 at 14:19
  • 2
    At a guess your input string contains some whitespace after 4095 characters so `cin` stops reading there. – Alan Birtles Jun 15 '19 at 14:19
  • If possibly we should just be able to copy-paste the [mcve] and build it without having to write add anything or to guess about anything. The same with input and how you run the program, how to you build the program (flags and options etc.)? How do you run the program? How do you generate the input? – Some programmer dude Jun 15 '19 at 14:19
  • Can you try using `cin.getline()` instead of `cin`? – kiner_shah Jun 15 '19 at 14:19
  • @πάνταῥεῖ Sorry please can you expalin me why do I need to write the string to a file and then read a file, when I just want to know the length of that big string? Is there some constraint on string `length` or `cin` which I am missing.and it deliberately has to be read from a (say text) file? – RC0993 Jun 15 '19 at 14:21
  • 1
    @kiner_shah sure I will try and get back to you with that – RC0993 Jun 15 '19 at 14:22
  • @RetiredNinja Yes It feels like some kind of limit is being reached – RC0993 Jun 15 '19 at 14:23
  • 1
    @AlanBirtles Hey Yes I though so too. so I manually checked the string at `4094 - 4097` characters... MANUALLY. and there is no whitespace especially not '\n' or '\0' they are all `...xxxx...` – RC0993 Jun 15 '19 at 14:25
  • I don't think there should be any limit problem: See this http://www.cplusplus.com/reference/string/string/max_size/ – kiner_shah Jun 15 '19 at 14:27
  • Can someone check [this link](https://hr-testcases-us-east-1.s3.amazonaws.com/63/input11.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1560609319&Signature=24xre8nA1iS%2BUpgH7SdlZwrVbaA%3D&response-content-type=text%2Fplain) if it is accessible other than me. (this is the link to the test case) – RC0993 Jun 15 '19 at 14:27
  • 1
    You want to try [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) to read your std::strings. The other function [`cin.getline()`](https://en.cppreference.com/w/cpp/io/basic_istream/getline) only reads `char` arrays. – Blastfurnace Jun 15 '19 at 14:30
  • I made some very bizzare changes and got `100001` as length. Please check the Edited post. – RC0993 Jun 15 '19 at 14:42
  • I would write your testcase to a file and redirect that to the program. `program < file.txt` In Windows if I paste a large amount of data I get a string of 4095 length, but redirected from a file it is the full amount. – Retired Ninja Jun 15 '19 at 14:56
  • `cin >> s` is formatted extraction. It reads a word. You have a whitespace at 4095 characters in. Don't use formatted extraction when you don't want it. Solution in the dupe. – Lightness Races in Orbit Jun 15 '19 at 14:57
  • Undoing the EDIT 1 in the post to avoid confusion for future readers – RC0993 Jun 15 '19 at 15:06
  • @LightnessRacesinOrbit Assuming question text is correct, it is unlikely there is whitespace after 4095 chars. Instead, there is EOF, most likely. IOW, the code is already reading the entire input. – hyde Jun 15 '19 at 22:08

1 Answers1

0

Assuming you describe the input correctly, that is it is one single "word", then the issue is not in your code. The issue must be in the environment which runs the code. It has some kind of mechanism to feed the standard input to your program. Either that has a limitation on total input length, or it has a limitation of line length. 4 kilobytes is 4096 bytes, so perhaps your input is limited by that: 4095 chars of the word plus a newline character (or terminating 0 byte of string, or whatever).

If you are running this under some kind of web interface in browser, the problem could even be, that the input field in the web page has that limitation.

If you need to dig into this, try to read char by char and see what you get, how many chars and how many newlines. Also examine cin.fail(), cin.eof(), cin.bad() and cin.good(). For the question code, you should expect failbit to be false, and eofbit might be true oe false depending on how the input was truncated.

hyde
  • 50,653
  • 19
  • 110
  • 158
  • Thanks for the insightful answer. You you be able to elaborate on how do I check if there is any such environmental constraints? And as you said `try to read char by char` do you mean I should input 4096+ characters manually one by one? – RC0993 Jun 15 '19 at 14:44
  • Char by char, yes, in a loop, using this, probably: http://www.cplusplus.com/reference/istream/istream/get/ (there's also example code behind that link). – hyde Jun 15 '19 at 14:46
  • No, the issue _is_ the code. `cin >> s` reads one token, not everything to EOF. – Lightness Races in Orbit Jun 15 '19 at 14:55
  • 1
    @LightnessRacesinOrbit Unless the input contains exactly one token, such as in the case described in the question. – eerorika Jun 15 '19 at 15:00
  • @LightnessRacesinOrbit and eerorika Hey I am soory I am a noob here, what is a token? – RC0993 Jun 15 '19 at 15:04
  • @RC0993 https://en.wikipedia.org/wiki/Lexical_analysis#Token In Western languages, a token is [basically] a word. Things separated by space. So think of it as a word. But more generally, a token could be separated by anything. Like if we have "a|b|c|d" and we decide that the "|"s are delimiters, the string has four tokens: "a", "b", "c" and "d". – Lightness Races in Orbit Jun 15 '19 at 15:32
  • FWIW, if the stdlib implementation is doing this, [it's non-compliant](http://eel.is/c++draft/string.io#10). – Lightness Races in Orbit Jun 15 '19 at 15:34
  • @RC0993 added a bit of text about examining the state of cin. – hyde Jun 15 '19 at 22:01