Write a program to print distinct words in an input
along with their count in input in decreasing order of their
count..
Answer Posted / gururaj l j
Solution
/* This Program counts the distinct words in the input
string preceded each word by its count */
#include <stdio.h>
#include <string.h>
void DistinctWords(char s[ ]);
void main()
{
char s[100];
printf("Enter the String ");
gets(s);
if(strlen(s) <= 0)
printf("Please provide the input
string\n");
else
DistinctWords(s);
}
/* Purpose: This function counts the distinct words in the
input string preceded each word by its count
Input : String
Output : distinct words with its count
*/
void DistinctWords(char s[])
{
char words[50][50];
char distinct[50];
int i, j, l, k = 0, start, end, f = 1, m = 0, n =
0;
int count;
int countindex[20],cindex = 0;
for(l = 0,i = 0; s[i] != ' '; i++, l++)
words[k][i] = s[i];
words[k][i] = '\0';
k++;
start = i+1;
for(i = i+1; s[i]; i++)
{
if(s[i] == ' ')
{
end = i-1;
f = i+1;
for(j = 0, l = start; l <= end;
l++, j++)
words[k][j] = s[l];
words[k][j] = '\0';
k++;
start = i+1;
}
}
for(l = f, i = 0; s[l]; i++, l++)
words[k][i] = s[l];
words[k][i] = '\0';
for(i = 0;i <= k; i++)
{
count = 0;
for(j = i; j <= k; j++)
{
if((stricmp(words[i], words[j])) == 0)
count++;
}
f = 1;
for(j = 0; j < i; j++)
{
if(strcmp(words[i], words[j]) == 0)
{
f = 0;
break;
}
}
if(f == 1)
printf("\n\t%s Occur %d Times\n", words[i],
count);
}
}
| Is This Answer Correct ? | 18 Yes | 7 No |
Post New Answer View All Answers
What is the acronym for ansi?
Is return a keyword in c?
What is an lvalue?
Why are some ANSI/ISO Standard library routines showing up as undefined, even though I've got an ANSI compiler?
What is a union?
What is the purpose of the statement: strcat (S2, S1)?
Who invented b language?
You have given 2 array. You need to find whether they will
create the same BST or not.
For example:
Array1:10 5 20 15 30
Array2:10 20 15 30 5
Result: True
Array1:10 5 20 15 30
Array2:10 15 20 30 5
Result: False
One Approach is Pretty Clear by creating BST O(nlogn) then
checking two tree for identical O(N) overall O(nlogn) ..we
need there exist O(N) Time & O(1) Space also without extra
space .Algorithm ??
DevoCoder
guest
Posted 3 months ago #
#define true 1
#define false 0
int check(int a1[],int a2[],int n1,int n2)
{
int i;
//n1 size of array a1[] and n2 size of a2[]
if(n1!=n2) return false;
//n1 and n2 must be same
for(i=0;i
How can a string be converted to a number?
What is the difference between test design and test case design?
How do you print only part of a string?
What does printf does?
Write a program of advanced Fibonacci series.
What are void pointers in c?
Write a program to produce the following output: 1 2 3 4 5 6 7 8 9 10