I have a problem with reading empty string in c.I want to read string from the following -
1.ass
2.ball
3.(empty)
4.cat
but when i use gets() it does not treat (empty) as string[2].it read 'cat' as string[2].So how can i solve this problem.Which thing should i use?? plz someone help.
char str1[15002][12];
char str2[15002][12];
char s[25];
map<string,int> Map;
int main()
{
int ncase,i,j,n1,n2,count,Case;
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d",&ncase);
Case=1;
while(ncase>0)
{
Map.clear();
//this is the necessery part
scanf("%d %d\n",&n1,&n2);
count=0;
printf("n1=%d n2=%d\n",n1,n2);
for(i=0;i<n1;i++)
{
gets(str1[i]);
}
for(i=0;i<n2;i++)
{
gets(str2[i]);
}
//end of reading input
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
strcpy(s,str1[i]);
strcat(s,str2[j]);
if(Map[s]==0){
count+=1;
Map[s]=1;
}
//printf("%s\n",str1[i]);
}
}
printf("Case %d: %d\n",Case,count);
Case++;
ncase--;
}
return 0;
}
and input can be look like
i have given the code here.the input may be like
line1>1
line2>3 3
line3>(empty line)
line4>a
line5>b
line6>c
line7>(empty)
line8>b
And i expect
str1[0]=(empty).
str1[1]=a;
str1[2]=b;
and
str2[0]=c;
str2[1]=(empty);
str2[2]=b;
Ok,at last i found the problem.it is the line (printf("n1=%d n2=%d\n",n1,n2);)
which creates problem in taking input by gets().Instead of taking newline with the integer n1,n2, then i take newline as a ("%c",&ch) and then everything is okay.
Thanx everyone who ans me.Hope you will also help me later.