Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Post Lab

Course code: EEE105 Section:03

Exp. No.: 07

Exp. Name: String

St. Name: Forhanul Haque

St. ID: 2023-2-80-007

Group no.: 06

Partners’ IDs: 2023-1-80-071

2023-2-80-008
Date of
performance: Instructor’s
Comment and
Signature:
06-05-2024
Date of
Submission:
16-05-2024
Answer to the Question No: 01

The following C code is:


#include <stdio.h>
int main()
{
char line[100], EVEN[50], ODD[50];
int even_count = 0, odd_count = 0;
printf("Enter a sentence (letters and spaces only): ");
fgets(line, sizeof(line), stdin);

int words = 0;
for (int i = 0; line[i] != '\0'; i++) {
if (line[i] == ' ' && line[i - 1] != ' ') {
words++;
}
}
if (line[0] != ' ') {
words++;
}
printf("Number of words: %d\n", words);

for (int i = 0; line[i] != '\0'; i++) {


if (i % 2 == 0) {
if (line[i] != ' ') {
if (line[i] >= 'a' && line[i] <= 'z') {
EVEN[even_count++] = line[i] - 32;
} else {
EVEN[even_count++] = line[i] + 32;
}
}
} else {
if (line[i] != ' ') {
if (line[i] >= 'a' && line[i] <= 'z') {
ODD[odd_count++] = line[i] - 32;
} else {
ODD[odd_count++] = line[i] + 32;
}
}
}
}
EVEN[even_count] = '\0';
ODD[odd_count] = '\0';

printf("String Even[] is: %s\n", EVEN);


printf("String ODD[ ] is: %s\n", ODD);
return 0;
}
Output:

Comment:
Initially, a sentence is inputted into this program and is stored in the array line. The
sentence's word count is then calculated and displayed. The characters that are
indexed as EVEN and ODD are then stored in other arrays labeled EVEN and
ODD, respectively, and the cases are switched as instructed. The contents of the
ODD and EVEN arrays are finally shown independently.
Answer to the Question No: 02
The following C code is:
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORDS 1000


#define MAX_WORD_LEN 100

typedef struct {
char word[MAX_WORD_LEN];
int count;
} WordFrequency;

void toLowercase(char *str) {


for (int i = 0; str[i]; i++) {
str[i] = tolower(str[i]);
}
}

int findWord(WordFrequency words[], int wordCount, char *word) {


for (int i = 0; i < wordCount; i++) {
if (strcmp(words[i].word, word) == 0) {
return i;
}
}
return -1;
}
int main() {
char str[1000];
WordFrequency words[MAX_WORDS];
int wordCount = 0;
char *token;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';

toLowercase(str);
token = strtok(str, " ,.-?!");
while (token != NULL) {
int index = findWord(words, wordCount, token);
if (index == -1) {
strcpy(words[wordCount].word, token);
words[wordCount].count = 1;
wordCount++;
} else {
words[index].count++;
}
token = strtok(NULL, " ,.-?!");
}

int maxCount = 0;
int maxIndex = 0;
for (int i = 0; i < wordCount; i++) {
if (words[i].count > maxCount) {
maxCount = words[i].count;
maxIndex = i;
}
}

printf("Most used sequence/word in the string is: '%s' and it occurred: %d times.\n",
words[maxIndex].word, maxCount);

return 0;
}

Output:

Comment:
This code is a C program that determines the frequency of each word by tokenizing
a user-supplied string into words. It tokenizes the string using a straightforward
method based on spaces and frequently used punctuation (exclamation points,
question marks, dashes, and commas).

You might also like