Edit the code below a C program which reads the standard inp
(Edit the code below )a C program which reads the standard input and produces a list of all distinct words in a lexicographic order. We will consider a word to be any sequence of letters. All words must be transformed to the lowercase letters. You can assume that there are no more than 10,000 distinct words, and that each word is not longer than 25 characters.
The program should first read all the input. If it found any words longer than 25 letters, such words should be ignored; and if it found more than 10,000 distinct words after making the words lowercase, all additional words should be ignored.
After reading input, the list of words must be printed to the output in the lexicographic order, one word per line.
If any words were ignored because they were longer than 25 characters, the following warning message should be printed at the end:
WARNING: Words longer than 25 characters were ignored.
Similarly, if there were more than 10,000 distinct words, the following warning message should be printed at the end:
WARNING: More than 10,000 words. Some words are ignored.
If any of the above two warnings are printed, they must be separated by a blank line from the previous list of words, and if both warnings are printed, the first one (about 25 characters) should be printed first in a line, immediatelly followed by the second warning on the next line.
Sample Input
This is a sample input.
The program should first read all the input. If it found any words
longer than 25 letters, such words should be ignored; and if it found
more than 10,000 distinct words after making the words lowercase, all
additional words should be ignored.
A very long word: PNEUMONOULTRAMICROSCOPICSILICOVOLCANOCONIOSI
Sample Output
a
additional
after
all
and
any
be
distinct
first
found
if
ignored
input
is
it
letters
long
longer
lowercase
making
more
program
read
sample
should
such
than
the
this
very
word
words
WARNING: Words longer than 25 characters were ignored.
-----------------------------------------------------------------------
Edit the code to convert upper character to low.
I try to use tolow() and strlwr but it does not work. It says incompatible types when assigning to type ‘char[100]’ from type ‘int’
Also, I try
for(i=0;i<=strlen(words);i++){
if(words[i]>=65&&words[i]<=90)
words[i]=words[i]+32;
which is can not compare char with int in my code
So, Could you please compile and execute the code before submitting the answer to me
//Headers files
#include <stdio.h>
#include <string.h>
int main()
{
char words[100][100];
char ans[100][100];
char temp[100];
int n,i,j,x=0,flag1=0,flag2=0; //flags used to find word length > 25 or not,m\\
ore than 100000 words or not
int k = 0;
//prompt & read number of words to enter
printf(\"enter the number of words you want to enter (max 100) :\ \");
scanf(\"%d\", &n);
//read all words using loop
printf(\"enter the %d words (max length 100) :\ \", n);
for (i = 0; i <= n; i++)
{
fgets(words[i],1000,stdin);
}
// loop for check lengths & count of words
for (i = 0; i <=n; i++)
{
if (strlen(words[i])<25) //length check
{
strcpy(ans[k],words[i]);
k++;
}
else //if length crosses 25 set flag1
{
flag1=1;
}
x++;
if (x>10000) //if words count crosses 100000 set flag2
{
flag2=1;
break;
}
}
//loops for sorting words
for (i = 0; i < n-1; i++)
{
for (j = i+1; j < n; j++)
{
if (strcmp(ans[i], ans[j])>0)
{
strcpy(temp, ans[i]);
strcpy(ans[i], ans[j]);
strcpy(ans[j], temp);
}
}
}
printf(\"\ In lexicographical order: \ \");
for (i = 0; i <=n; i++)
{
puts(ans[i]);
if(flag1==1) //display error if flag1 set
printf(\"\ WARNING: Words longer than 25 characters were ignored.\ \");
if(flag2==1) //display error if flag2 set
printf(\"\ WARNING: More than 10,000 words. Some words are ignored.\ \");
return 0;
}
Solution
when I executed it was not compliling . There was one curly brace } was missing at the end ,, so after I placed it it complied ,
Added fflush(stdin); ,
//invalid string reduce the words count by 1 and set to null
if( flag1 || flag2 )
strcpy(ans[n-1] ,\"\\0\");
printf(\"%s\ \",ans[i]);
put these two lines out of for loop
if(flag1==1) //display error if flag1 set
printf(\"WARNING: Words longer than 25 characters were ignored.\ \");
if(flag2==1) //display error if flag2 set
printf(\"WARNING: More than 10,000 words. Some words are ignored.\ \");
working program below with some change..
------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
int main()
{
char words[100][100];
char ans[100][100];
char temp[100];
int n,i,j,x=0,flag1=0,flag2=0; //flags used to find word length > 25 or not,m\\ore than 100000 words or not
int k = 0;
//prompt & read number of words to enter
printf(\"enter the number of words you want to enter (max 100) :\ \");
scanf(\"%d\", &n);
//read all words using loop
printf(\"enter the %d words (max length 100) :\ \", n);
for (i = 0; i < n; i++)
{
fflush(stdin);
fgets(words[i],1000,stdin);
}
// loop for check lengths & count of words
for (i = 0; i <n; i++)
{
if (strlen(words[i])<25) //length check
{
strcpy(ans[k],words[i]);
k++;
}
else //if length crosses 25 set flag1
{
flag1=1;
}
x++;
if (x>10000) //if words count crosses 100000 set flag2
{
flag2=1;
break;
}
}
//loops for sorting words
for (i = 0; i < n-1; i++)
{
for (j = i+1; j < n; j++)
{
if (strcmp(ans[i], ans[j])>0)
{
strcpy(temp, ans[i]);
strcpy(ans[i], ans[j]);
strcpy(ans[j], temp);
}
}
}
printf(\"\ In lexicographical order: \ \");
//invalid string reduce the words count by 1 and set to null
if( flag1 || flag2 )
strcpy(ans[n-1] ,\"\\0\");
for (i = 0; i <n; i++)
{
printf(\"%s\ \",ans[i]);
}
if(flag1==1) //display error if flag1 set
printf(\"WARNING: Words longer than 25 characters were ignored.\ \");
if(flag2==1) //display error if flag2 set
printf(\"WARNING: More than 10,000 words. Some words are ignored.\ \");
return 0;
}





