In Linux 1) 10) Use strtok to extract words from a sentence and store them in an
ID: 3737934 • Letter: I
Question
In Linux
1)
10) Use strtok to extract words from a sentence and store them in an array. Display the number of words as below.
algorithm:
read a line
tokenize
display tokens
Enter a sentence
aa bcd e e ff aa bcd bcd hijk lmn al bcd
You entered aa bcd e e ff aa bcd bcd hijk lmn al bcd
There were 12 words:
aa
bcd
e
e
ff
aa
bcd
bcd
hijk
lmn
al
bcd
The original sentence was: aa bcd e e ff aa bcd bcd hijk lmn al bcd
2) What is wrong with the following program? How can you fix it?
void getstr(char *str){
printf(“enter a string ”);
scanf(“%s”, str);
}
int main(){
char * strarr[10]={NULL};
getstr(strarr[0]);
printf(“we got %s ”, strarr[0]);
3) Write a program that reads a long sentence and displays the frequency of each word as below. It also prints the word that has the maximum frequency. Use as many functions as possible to split your program into small pieces.
algorithm:
read a line
tokenize
display tokens
compute frequency
for each token
if it is already in freq[] array, increase its frequency
otherwise store in freq[] and initialize its frequency=1
display frequencies
compute max frequency word and display it
Enter a sentence
aa bcd e e ff aa bcd bcd hijk lmn al bcd
You entered aa bcd e e ff aa bcd bcd hijk lmn al bcd
There were 12 words: aa bcd e e ff aa bcd bcd hijk lmn al bcd
Frequncies: aa 2 bcd 4 e 2 ff 1 hijk 1 lmm 1 al 1
The word with the max freq: bcd
Explanation / Answer
1) PROGRAM
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int main()
{
char *str,*tok,*cstr,*ccstr; // declare pointer with character variables
int count=0; // declare and initialize integer variable count=0
// allocate memory for all pointer variables
str=(char *)malloc(sizeof(char));
tok=(char *)malloc(sizeof(char));
cstr=(char *)malloc(sizeof(char));
ccstr=(char *)malloc(sizeof(char));
printf(" Enter a sentence ");
scanf("%[^ ]",str); // read sentense
// copy original sentence to cstr and ccstr
strcpy(cstr,str);
strcpy(ccstr,str);
printf(" You entered %s",str); // display original sentence
tok=strtok(str," "); // take token with delimeter " " str
while(tok!=NULL) // create while loop until end of sentence
{
count++; // counting number of words in the sentence
tok=strtok(NULL," "); // null word
}
printf(" There were %d words",count); // display total words in the sentence
tok=strtok(cstr," "); // take token with delimeter of " " str, because str is copied to cstr
while(tok!=NULL)
{
printf(" %s",tok); // display tokens
tok=strtok(NULL," ");
}
printf(" The original sentence was: %s",ccstr); // display original string ccstr copied from str
return 0;
}
OUTPUT
Enter a sentence
aa bcd e e ff aa bcd bcd hijk lmn al bcd
You entered aa bcd e e ff aa bcd bcd hijk lmn al bcd
There were 12 words
aa
bcd
e
e
ff
aa
bcd
bcd
hijk
lmn
al
bcd
The original sentence was: aa bcd e e ff aa bcd bcd hijk lmn al bcd
2) PROGRAM
#include<stdio.h>
#include<string.h>
// create function getstr
void getstr(char *str)
{
printf("enter a string ");
scanf("%s",str);
}
int main()
{
char *strarr[10]={NULL}; // declare pointer with array characters with NULL
int i;
for(i=0;i<10;i++)
strarr[i]=(char *)malloc(sizeof(char)); // allocate memory
getstr(strarr[0]); // call getstr() function
printf("we got %s ",strarr[0]); // display strarr[0]
return 0;
}
OUTPUT
enter a string
Welcome
we got Welcome
3) PROGRAM
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include<malloc.h>
struct word {
char str[20]; // word string: assume max 19 characters
int count; // word count
} ;
// calculate frequency of words in a given string
int word_freq(char *str, struct word words[])
{
char *tmp_str; // pointer to a copy of given string
char *wptr; // pointer to a word
int nword; //number of distinct words
int i,counter=0; // initialize counter is zero
nword = 0; // initialize nword is zero
tmp_str = strdup(str); // copy of given string
wptr = strtok(tmp_str, " "); // get ptr to first word
while (wptr != NULL)
{
counter++; // count number of tokens
// search current word in 'words' array
for(i = 0; i < nword; i++)
{
if (strcmp(wptr, words[i].str) == 0)
break; // current word found, stop search
}
// if current word is not in words array, add it at loc nword
if (i < nword) // current word already in 'words' array
words[i] .count++; // increment its count
else { // current word not in 'words' array
strcpy(words[nword].str, wptr); //add word at pos. nword
words[nword].count= 1; // set freq count to 1
++nword; // increment words count
}
wptr = strtok(NULL, " "); // get ptr to next word
}
printf(" There were %d words: %s ",counter,str);
free(tmp_str); // release memory allocated to tmp_str
return nword;
}
int main ()
{
char str[40],maxword[10]=""; // assume str length 40
struct word words[100]; //assume max. 100 distinct words
char *tok;
int nword; // no of words
int i,max=0;
printf(" Enter sentense ");
scanf("%[^ ]",str); // read sentense
printf("You enteered %s ", str); // display original sentense
nword = word_freq(str, words);
printf(" Frequencies: ");
for(i = 0; i < nword; i++){
printf(" %s %d ", words[i].str, words[i] .count); // display words and frequencies
}
for(i=0;i<nword;i++)
{
if(words[i].count>max) // check maximum word
{
max=words[i].count;
strcpy(maxword,words[i].str); // copy maximum word into maxword
}
}
printf(" The word with the max frq: %s",maxword); // display maxword
return 0;
}
OUTPUT
Enter sentense
aa bcd e e ff aa bcd bcd jijk lmn al bcd
You enteered aa bcd e e ff aa bcd bcd jijk lmn al bcd
There were 12 words: aa bcd e e ff aa bcd bcd jijk lmn al bcd
Frequencies: aa 2 bcd 4 e 2 ff 1 jijk 1 lmn 1 al 1
The word with the max frq: bcd
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.