Sir, i'm new here searched the forums and web.
I'm a beginner and dont know C very well.
The Question actually is this:
Write a C function called DistinctWords that counts the distinct words in its input string and prints them in the descending order of number of its occurrence. Precede each word by its count. Write a main function to test your code with different input values to demonstrate that your function is robust.
The function signature in C is as follows:
void DistinctWords(char s[]);
Thought strtok() might help and wrote this coe:
# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h> // for "system("cls");" to clear screen
void distintwords(char s[]);
int main()
{
while(1)
{
system("cls");
char s[1000],x;
printf("Enter the string: \n");
gets(s);
distintwords(s);
printf("\n\nPress ENTER to try again, 'q' to quit : ");
x = getch();
if (x=='q')
break;
}
}
void distintwords(char s[])
{
int slen,i,j,k;
char * tok;
char * toks[20];
slen == strlen(s);
tok = strtok (s," ,.-?!");
while (tok != NULL)
{
printf ("%s\n",tok);
tok = strtok (NULL," ,.-?!");
for(i=1;i<10;i++)
{
toks[i]=tok;
printf ("Test: %s\n",tok[i]);
}
}
}
The above code i'm not knowing how to allot each word to different variable like tok[1],tok[2], etc so i can compare.
The other code tried by my friend is:
# include <stdio.h>
# include <conio.h>
#include<string.h>
void distintwords(char s[]);
int main()
{
char s[100];
printf("enter the string");
gets(s);
distintwords(s);
getch();
}
void distintwords(char s[])
{
int arr[20],p1=0,p2=0,count=1,n;
char str[20][20];
int i=0,k=0,v=0;
for(i=0;s[i]!='\0';i++)
{
k=0;
static int j=0;
for(;s[j+i]!='\0'&& s[j+i]!=' ';j++)
{
str[v][k]=s[j+i];
k++;
}
str[v][k]='\0';
v++;
}
int words=0;
int c=0;
while(s[c]!='\0')
{
if(s[c]==' ')
words++;
c++;
}
printf("%d\n",words+1);
for(i=0;i<words;i++)
{
arr[i]= strlen(str[i]);
}
for(i=0;i<words;i++)
{
for(k=0;k<words;k++)
{
if(arr[i]==arr[k])
n= strcmp(str[i],str[k]);
if(n==0)
{
count++;
printf("%d %s",count,str[i]);
}
else {
for(i=0;i<=words;i++)
printf("\n %d %s",count,str[i]);
}
}
}
}
Both outputs are very annoying.
So for now i'm left with this code:
# include <stdio.h>
# include <conio.h>
# include<string.h>
# include <stdlib.h> // for "system("cls");" to clear screen
void distintwords(char s[]);
int main()
{
while(1)
{
system("cls");
char s[1000],x;
printf("Enter the string: \n");
gets(s);
distintwords(s);
printf("\n\nPress ENTER to try again, 'q' to quit : ");
x = getch();
if (x=='q')
break;
}
}
void distintwords(char s[])
{
int slen,i,j,k;
char * tok;
char * toks[20];
slen == strlen(s);
tok = strtok (s," ,.-?!");
while (tok != NULL)
{
printf ("%s\n",tok);
tok = strtok (NULL," ,.-?!");
for(i=1;i<10;i++)
{
toks[i]=tok;
printf ("Test: %s\n",tok[i]);
}
}
}
/*
int arr[20],p1=0,p2=0,count=1,n;
char str[20][20];
int i=0,k=0,v=0;
for(i=0;s[i]!='\0';i++)
{
k=0;
static int j=0;
for( ;s[j+i]!='\0'&& s[j+i]!=' ';j++)
{
str[v][k]=s[j+i];
k++;
}
str[v][k]='\0';
v++;
}
int words=0;
int c=0;
while(s[c]!='\0')
{
if(s[c]==' ')
words++;
c++;
}
printf("Total Number of Words: %d \n \n",words+1);
for(i=0;i<words;i++)
arr[i]= strlen(str[i]);
for(i=0;i<words;i++)
for(k=0;k<words;k++)
if(arr[i]==arr[k])
{
n = strcmp(str[i],str[k]);
if(n==0)
{ count++;
printf("%s : %d \n",str[i],count);
}
else
{
//for(i=0;i<=words;i++)
printf("%s : %d \n",str[k],count-1);
}
}
}
*/
Sorry for the mess.