This mini-program has to be written in C language (NOT Java or C++). It\'s very
ID: 3625034 • Letter: T
Question
This mini-program has to be written in C language (NOT Java or C++). It's very straightforward and simple! The instructions are within the code, add the codes right after the every instruction below.
#include
#include
#define WORD_LENGTH 10
#define NUM_WORDS 10
int containsSubstring(const char *, const char *);
int countVowels(const char *);
int tokenize(const char *, char [][WORD_LENGTH]);
int main() {
char line[NUM_WORDS * WORD_LENGTH];
int i = 0;
// 1. Read a line from the standard input and store it in the 'line' variable
printf("1. Enter a line of text: ");
/*
HINT: take a look at the getchar() function
*/
// 2. Count the number of vowels in that line
printf("2. In the line '%s' there are %d vowels ", line, countVowels(line));
// 3. Parse the line and print out a word per line
char words[NUM_WORDS][WORD_LENGTH];
int numWords = tokenize(line, words);
printf("3. The %d words in the line are: ", numWords);
for (i = 0; i < numWords; i++) {
printf(" '%s' ", words[i]);
}
// 4. Read a substring from the standard input and print all words that contain that substring
printf("4. Enter a substring: ");
char substring[WORD_LENGTH];
scanf("%s", substring);
for (i = 0; i < numWords; i++) {
if (containsSubstring(words[i], substring) == 0) {
printf(" '%s' contains '%s' ", words[i], substring);
}
}
return 0;
}
/*
* Determine if the string s1 contains any occurrence of the string s2.
* If one occurrence is found it returns 0, otherwise it returns -1.
*/
int containsSubstring(const char *s1, const char *s2) {
int result = -1;
return result;
}
/*
* Determine the number of occurrences of a vowel in the string s1.
*/
int countVowels(const char *s1) {
int counter = 0;
return counter;
}
/*
* Store all the words that are in the string s1 into the array s2.
* Return the number of words found in the string s1.
*/
int tokenize(const char *s1, char s2[][WORD_LENGTH]) {
int wordCounter = 0;
return wordCounter;
}
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
#define WORD_LENGTH 10
#define NUM_WORDS 10
int containsSubstring(const char *, const char *);
int countVowels(const char *);
int tokenize(const char *, char [][WORD_LENGTH]);
int main() {
char line[NUM_WORDS * WORD_LENGTH];
int i = 0;
char l;
// 1. Read a line from the standard input and store it in the 'line' variable
printf("1. Enter a line of text: ");
do
{line[i]=getchar();
if(line[i]==' ')
line[i]='';
i++;
}while(line[i-1]!='');
/*
HINT: take a look at the getchar() function
*/
// 2. Count the number of vowels in that line
printf("2. In the line '%s' there are %d vowels ", line, countVowels(line));
// 3. Parse the line and print out a word per line
char words[NUM_WORDS][WORD_LENGTH];
int numWords = tokenize(line, words);
printf("3. The %d words in the line are: ", numWords);
for (i = 0; i < numWords; i++) {
printf(" '%s' ", words[i]);
}
// 4. Read a substring from the standard input and print all words that contain that substring
printf("4. Enter a substring: ");
char substring[WORD_LENGTH];
scanf("%s", substring);
for (i = 0; i < numWords; i++) {
if (containsSubstring(words[i], substring) == 0) {
printf(" '%s' contains '%s' ", words[i], substring);
}
}
getch();
return 0;
}
/*
* Determine if the string s1 contains any occurrence of the string s2.
* If one occurrence is found it returns 0, otherwise it returns -1.
*/
int containsSubstring(const char *s1, const char *s2) {
int result = -1;
int i=0;
int j=0;
int m,k;
while(*(s1+j)!='')
{if(*(s2+i)==*(s1+j))
{k=j+1;
for(m=i+1;*(s1+m)!='';m++)
{if(*(s1+m)==*(s2+k))
k++;
else
{m=99;
j++;
}
}
if(m!=99)
return 0;
}
else
j++;
}
return result;
}
/*
* Determine the number of occurrences of a vowel in the string s1.
*/
int countVowels(const char *s1) {
int counter = 0;
int j=0;
char l;
do
{l=toupper(*(s1+j));
if(l=='A'||l=='E'||l=='I'||l=='O'||l=='U')
counter++;
j++;
}while(*(s1+j)!='');
return counter;
}
/*
* Store all the words that are in the string s1 into the array s2.
* Return the number of words found in the string s1.
*/
int tokenize(const char *s1, char s2[][WORD_LENGTH]) {
int wordCounter = 0;
int j=0,k=0;
char l;
do
{l=*(s1+j);
if (isspace(l))
{
s2[wordCounter][k]='';
wordCounter++;
k=0;
}
else
s2[wordCounter][k++]=l;
j++;
}while(*(s1+j)!='');
s2[wordCounter][k]='';
wordCounter++;
return wordCounter;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.