The program should first read all the input. If it found any words longer than 2
ID: 3775716 • Letter: T
Question
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
Sample Output
-------------------------------------------------------------------------------------------------------------------------
Some one help me and write this code but It dosent work correctly with me
I need to read from standard input not from file if any one can edite the code
Thank you.
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
char c[100][100];
char ans[100][100];
char temp[100];
int i=0, j=0, l=0, k=0;
FILE *fptr;
if ((fptr = fopen("test.txt", "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}
while (fscanf(fptr, " %[^ ] ", c[i] ) != EOF) {
i++;}
for(j=0;j {
if(strlen(c[j])<25)
{
{
strcpy(ans[k],c[j]);
k=k+1;
}
if(j>10000)
break;
}
for(i=0; i for(j=i+1; 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 {
puts(ans[i]);
}
printf("WARNING: Words longer than 25 characters were ignored.");
fclose(fptr);
return 0;}}
--------------------------------------------------------------------------------------------------------
OR this one works correctly but I need to chang the words from upper to lower
//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,more 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;
}
Explanation / Answer
//Headers files
#include <stdio.h>
#include <string.h>
#include<ctype.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,more 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++)
{
int j=0;
while(words[i][j]!=''){
if(words[i][j]>='A' && words[i][j]<='Z')
words[i][j]=words[i][j]+32;
j++;
}
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.