Fast string input

What is the fastest way to input strings in c and c++?

I know there are many ways to get integers fast but I was not able to find anything that helps with strings especially char strings.

I don’t know which one is fastest. But, for contest point of view. Both gets() and scanf("%s",a) serves their purpose well and you’ll never get TLE because of them.

In my opinion gets is faster than scanf in case of strings.

You can take input char by char…using getchar_unlocked()…which is quite fast…maybe faster than scanf & gets…hope this helps…:slight_smile:

EDIT:

maybe this code might be of some use…link!!!

you can use string class in c++ http://www.cplusplus.com/reference/string/string/
to get input of string it is the fastest way i came around it is faster than scanf and gets

Try this question

I have used fread function to get answer within the time limit.
For fread usage read this: http://www.cplusplus.com/reference/cstdio/fread/
Last field can be set to STDIN if you are reading from standard input rather than a file

In my opinion gets or getchar_unlocked() is fastest way to get string input

One could use http://www.cplusplus.com/reference/cstdio/gets/
to read into a character array but its deprecated, or fgets where you can specify max number of characters to avoid overflow

I think that gets() is faster than scanf because it doesn’t put ‘\0’ in the end of the string

use scanf and printf for all types of data.
:slight_smile: .

for reading strings, read() is much much faster than scanf()

We can create a gets function which I think should be the fastest way

void gets_scratch(char str[]){

int i =0;

char a;

do{

a = getchar();

if(int(a)!=10){

str[i] = a;

++i;

}

else a[i] = ‘\0’;

}while(int(a)!=10);

}

Please correct me if I am wrong