Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read empty string in C
    primarykey
    data
    text
    <p>I have a problem with reading empty string in C. I want to read string from the following -</p> <ol> <li>ass</li> <li>ball</li> <li>(empty)</li> <li>cat</li> </ol> <p>but when I use <code>gets()</code> it does not treat <code>(empty)</code> as string[2]. It reads 'cat' as string[2]. So how can I solve this problem?</p> <pre><code>char str1[15002][12]; char str2[15002][12]; char s[25]; map&lt;string,int&gt; Map; int main() { int ncase, i, j, n1, n2, count, Case; freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); scanf("%d",&amp;ncase); Case = 1; while(ncase &gt; 0) { Map.clear(); //this is the necessery part scanf("%d %d\n",&amp;n1,&amp;n2); count = 0; printf("n1=%d n2=%d\n",n1,n2); for(i = 0; i &lt; n1; i++) { gets(str1[i]); } for(i = 0; i &lt; n2; i++) { gets(str2[i]); } //end of reading input for(i = 0; i &lt; n1; i++) { for(j = 0; j &lt; n2; j++) { strcpy(s,str1[i]); strcat(s,str2[j]); if(Map[s] == 0){ count += 1; Map[s] = 1; } } } printf("Case %d: %d\n", Case, count); Case++; ncase--; } return 0; } </code></pre> <p>and input can look like</p> <p>I have given the code here. The input may be like</p> <pre><code>line1&gt;1 line2&gt;3 3 line3&gt;(empty line) line4&gt;a line5&gt;b line6&gt;c line7&gt;(empty) line8&gt;b </code></pre> <p>And I expect</p> <pre><code>str1[0]=(empty). str1[1]=a; str1[2]=b; </code></pre> <p>and</p> <pre><code>str2[0]=c; str2[1]=(empty); str2[2]=b; </code></pre> <p><br><br><br> OK, at last I found the problem. It is the line </p> <pre><code>printf("n1=%d n2=%d\n",n1,n2); </code></pre> <p>which creates problem in taking input by <code>gets()</code>. Instead of taking newline with the integer <code>n1</code>, <code>n2</code>, then I take newline as a (<code>"%c",&amp;ch</code>) and then everything is okay.</p> <p>Thanks to everyone who answered me.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload