You are not logged in. Please login at www.codechef.com to post your questions!

×

string length

how to input a string of length n where 1<=n<=100000 ?

asked 09 Nov '14, 01:48

arshit's gravatar image

1★arshit
112
accept rate: 0%


The safer and better way to implement it is using Dynamic Arrays - realloc() !

char *enterString(FILE* fp, size_s strs)
{
char *str;
int ch;
size_s len = 0;
str = realloc(NULL, sizeof(char)*strs);
if(!str)return str;
while(EOF!=(ch=fgetc(fp)) && ch != '\n'){
    str[len++]=ch;
    if(len==size){
        str = realloc(str, sizeof(char)*(size+=16));
        if(!str)return str;
    }
}
str[len++]='\0';

return realloc(str, sizeof(char)*len);

}

In main() function,

s = enterString(stdin, 10);
printf("%s", s);

free(m);
link

answered 09 Nov '14, 21:11

joyjophin's gravatar image

0★joyjophin
1104
accept rate: 16%

@arshit you can use

In c: char str[n+1]; gets(str);

In c++: string str; getline(cin, str);

link

answered 09 Nov '14, 14:19

anki_2189's gravatar image

3★anki_2189
1
accept rate: 0%

1

we need to take care of the fact that gets() leave a '\n' character in the i/p stream and getline doesnot.

(09 Nov '14, 15:47) damn_me3★

You can better accept a single character using a getchar() and make your string the way, str[idx++] = c; where c was your i/p till you find your ending character.

link

answered 09 Nov '14, 15:46

damn_me's gravatar image

3★damn_me
2.6k21336
accept rate: 24%

Try this. Read the explanation here.

link

answered 09 Nov '14, 21:53

gautam94's gravatar image

3★gautam94
474142646
accept rate: 11%

@arshit the best and easy way to do it is :

In C:-

char a[100000];

scnaf("%s",&a); or gets(a);

In C++ :-

char a[100000];

cin>>a;

link

answered 10 Nov '14, 00:46

ashwanimk's gravatar image

3★ashwanimk
129125
accept rate: 3%

thank u everybody!!

link

answered 11 Nov '14, 02:15

arshit's gravatar image

1★arshit
112
accept rate: 0%

Please accept answer, to close the thread.

(11 Nov '14, 11:13) tech_boy2★

If you are sure about the value of n, i.e. you know for sure the number of characters forming the string,the fread is the appropriate function to use. It picks the next n characters from the stream with no regard to any sort of whitespaces(space,newline,tab etc).This means it picks them all. The syntax is-

char a[n]; fread(a,1,n,stdin);

'a' is the data array. '1' is the size in bytes of the data block you are storing in a. 'n' is the number of data blocks to pick. 'stdin' can be used interchangeably with whatever stream you are using.

link

answered 11 Nov '14, 09:16

ashishn42's gravatar image

3★ashishn42
105234
accept rate: 0%

edited 11 Nov '14, 09:17

-1

in c

char s[n + 1]; scanf("%s", s);


in c++

string s; cin>>s;

[edit] for spaces using gets may be dangerous better to use scanf with modifiers or write your own get() using getchar

link

answered 09 Nov '14, 04:30

vishfrnds's gravatar image

6★vishfrnds
47128
accept rate: 0%

edited 09 Nov '14, 15:19

1

Neither of the approaches will work if the string contains an embedded space

(09 Nov '14, 06:09) holmessherlock3★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×643
×2

question asked: 09 Nov '14, 01:48

question was seen: 5,016 times

last updated: 11 Nov '14, 11:13