I need to find the number of occurrences of a word in 3 strings. For example, if I type the following:
Enter a line of text: apple sauce
Enter a line of text: is
Enter a line of text: apple sauce
Word Occurrence
apple 2
sauce 2
is 1
My code so far:
#define _CRT_SECURE_NO_WARNINGS
#define ROWS 3
#define COLS 80
#define SIZE 50
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include<ctype.h>
int main()
{
char sentences[ROWS][COLS] = { "" };
int counter[SIZE] = { 0 };
for (size_t i = 0; i < ROWS; i )
{
printf("%s", "Enter a line of text: ");
fgets(sentences[i], COLS, stdin);
sentences[i][strlen(sentences[i]) - 1] = '\0';
}
for (size_t i = 0; i < ROWS; i )
{
char* tokens = strtok(sentences[i], " ");
while (tokens != NULL)
{
format(tokens);
tokens = strtok(NULL, " ");
}
}
system("PAUSE");
return 0;
}
void format (char* t)
{
for (size_t i = 1; i < strlen(t); i )
{
if (!(isalpha(t[i])))
{
t[strlen(t) - 1] = '\0';
}
}
for (size_t i = 0; i < strlen(t); i )
{
t[i] = tolower(t[i]);
}
}
CodePudding user response:
My guess is that you are getting an error like this
Severity Code Description Project File Line Suppression State Warning C4013 'format' undefined; assuming extern returning int ConsoleApplication3 C:\work\ConsoleApplication3\ConsoleApplication3.cpp 58
The fix is this. Add this line
void format (char* t);
just before
int main()
c is remarkably stupid, it does not like surprises. It needs to have a declaration of everything before you are allowed to reference it. THats why those #includes have to go at the beginning , they are crammed with function declarations of stuff like printf, gets, scanf....
CodePudding user response:
if i want solve your problem it's my answer
#define _CRT_SECURE_NO_WARNINGS
#define ROWS 3
#define COLS 80
#define SIZE 50
#define MAX_WORDS 50
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include<ctype.h>
typedef struct {
char Text[32];
int Count;
} Word;
typedef struct {
Word List[MAX_WORDS];
int Length;
} Words;
int main()
{
char sentences[ROWS][COLS] = { "" };
int counter[SIZE] = { 0 };
Words words = {0};
// get sentences
for (size_t i = 0; i < ROWS; i )
{
printf("%s", "Enter a line of text: ");
fgets(sentences[i], COLS, stdin);
sentences[i][strlen(sentences[i]) - 1] = '\0';
}
// count words
for (size_t i = 0; i < ROWS; i )
{
char* tokens = strtok(sentences[i], " ");
while (tokens != NULL)
{
count_word(&words, tokens);
tokens = strtok(NULL, " ");
}
}
// show words
puts("Word Occurrence");
for (size_t i = 0; i < words.Length; i ) {
printf("%-15s%d\n", words.List[i].Text, words.List[i].Count);
}
system("PAUSE");
return 0;
}
void to_lower(char* str) {
while (*str != '\0') {
if ('A' <= *str && 'Z' >= *str) {
*str = *str 32;
}
str ;
}
}
void count_word(Words* words, char* t)
{
// to lower
to_lower(t);
// check word exits
bool result = false;
for (int i = 0; i < words->Length; i ) {
if (strcmp(words->List[i].Text, t) == 0) {
// found it
words->List[i].Count ;
result = true;
}
}
// check for new word
if (!result) {
strcpy(words->List[words->Length].Text, t);
words->List[words->Length].Count ;
words->Length ;
}
}