Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include"header.

h"

void input(char a[]) {


cin.getline(a, 1025);
}

int countWords(char a[])


{
bool space_flag = true;
int count = 0;

for (int i = 0; i < strlen(a); i++) {


if (a[i] == ' ' || a[i] == '\t')
{
space_flag = true;
}
else if (space_flag) {
count = count + 1;
space_flag = false;
}
}
return count;
}

void separateLineWords(char a[]) {


bool space_flag = true;

for (int i = 0; i < strlen(a); i++) {


if (a[i] == ' ' || a[i] == '\t')
{
if (!space_flag) // ky tu truoc do khong la space thi xuong dong
cout << endl;
space_flag = true;
}
else {
cout << a[i];
space_flag = false;
}
}
}

int findLongestWord(char a[], char longest_word[]) {


int max_len = 0;
int start = 0, end = 0;
for (int i = 0; i < strlen(a) - 1; i++) {
if (a[i] == ' ' || a[i] == '\t')
{
if (a[i + 1] != ' ' && a[i + 1] != '\t') {
start = i + 1;
for (int j = start + 1; j < strlen(a); j++) {
if (a[j] == ' ')
{
end = j - 1;
break;
}
else if (j == strlen(a) - 1)
{
end = j;
break;
}
}
int tmp = end - start + 1;
if (tmp > max_len)
max_len = tmp;
}
}
}
for (int i = start; i <= end; i++) {
longest_word[i - start] = a[i];
}
longest_word[max_len] = '\0';
return max_len;
}

You might also like