I'm writing a program that works with a string, converts it into an array of words, and stuff. Initially, I wrote it like
main()
{ char a[100];
char *s;
int len;
getchar();
printf("\nEnter string: ");
gets(a);
for(len=0;*(a+len)!='\0';len++);
printf("\nLength is %d\n",len);
s=malloc(sizeof(char)*len);
s=a;
//Rest of the program
}
and I used the string s for the rest of the program. It worked fine. The rest of the program separates words and prints them, and I could see the output.
Then I thought a was redundant and wrote
main()
{ char *s;
int app_len;
printf("\nEnter length of string(Approximate more than necessary): ");
scanf("%d",&app_len);
getchar();
printf("\nEnter string: ");
s=malloc(sizeof(char)*app_len);
fgets(s,app_len+1,stdin);
//Rest of the program
}
When I ran the second version, and gave a valid input, there was no output, the command-prompt returned to the windows-equivalent of the dollar prompt.
Is something wrong with the way I used malloc; or fgets? After allocating memory space using malloc, am I only allowed to assign a value to it, and not to take input?
EDIT: Here's all my code. It works now :)
#include<stdio.h>
#include<stdlib.h>
void copy(char *dest,char *s)
{ int i;
for(i=0;*(s+i)!='\0';i++)
*(dest+i)=*(s+i);
*(dest+i)='\0';
}
int comp(char *s1,char *s2)
{ int i;
for(i=0;!(*(s1+i)=='\0'||*(s2+i)=='\0');i++)
if(*(s1+i)!=*(s2+i))
return -1;
return 0;
}
int length(char *s)
{ int i;
for(i=0;*(s+i)!='\0';i++);
return i;
}
main()
{ char b[20][20],words[20][20],unique[20][20],non[20][20];
int i,j,k,len,no_of_words, c,count[100],w=0,u=0,nu=0;
int mem = 64;
char *s = malloc(mem);
printf("\nEnter string : ");
fgets(s, mem, stdin);
while(s[length(s) - 1] != '\n') //checks if we ran out of space
{ mem *= 2;
s = realloc(s, mem); //double the amount of space
fgets(s + mem / 2 - 1, mem / 2 + 1, stdin);//read the rest (hopefully) of the line into the new space.
}
len=length(s);
*(s+len--)='\0';
printf("%s",s);
for(i=0,j=0,k=0;i<len&&*(s+i)!='\0'&&*(s+i)!='\n';i++)
{ if(*(s+i)==' ')
{ *(*(b+j)+k)='\0';
j++;
k=0;
}
else
{
*(*(b+j)+k)=*(s+i);//jth word, position k
k++;
}
}
*(*(b+j)+k)='\0';
free(s);
no_of_words=j+1;
for(i=0;i<no_of_words;i++)
{ for(j=0,c=0;j<w;j++)
{ if(comp(*(b+i),*(words+j))==0)
c++;
}
if(c==0)
{ copy(*(words+w),*(b+i));
w++;
}
}
for(j=0;j<w;j++)
{ for(i=0,c=0;i<no_of_words;i++)
{ if(comp(*(b+i),*(words+j))==0)
c++;
}
if(c!=0)
{ copy(*(non+nu),*(words+j));
count[nu]=c;
nu++;
}
if(c==1)
{ copy(*(unique+u),*(words+j));
u++;
}
}
for(i=0,c=0;i<nu;i++)
if(count[i]!=1)
c++;
printf("\nTotal no. of words = %d\n",no_of_words);
printf("No. of distinct words = %d\n",w);
printf("No. of distinct unique words = %d\n",u);
printf("No. of distinct non-unique words = %d\n",c);
printf("\nUnique words:\n");
for(i=0;i<u;i++)
{ printf("%s\n",*(unique+i));
}
printf("\nNon-unique words:\n");
for(i=0;i<nu;i++)
if(count[i]!=1)
printf("%s\tFrequency=%d\n",*(non+i),count[i]);
printf("\nList of all words and their frequencies:\n");
for(i=0;i<nu;i++)
printf("%s\tFrequency=%d\n",*(non+i),count[i]);
}
sizeof(char)in malloc. It's defined as 1.s=a;usestrcpy()instead, avoid usinggets()usefget()app_len, then you may only inputapp_len-1characters into the string and the argument tofgetsshould also beapp_len.s = strpy(malloc(len+1), a);