Найти самое длинное слово в тексте - C (СИ)
Формулировка задачи:
Помогите найти ошибки в программе для нахождения самого длинного слова в тексте. Что нужно добавить, чтобы данная программа находила и самую длинную фразу?
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char string[]="C is one of the most widely used programming languages of all time";
char geps[]=" ";
char *p1;
char *p2;
int max_dlinna;
max_dlinna=0;
printf("%s",string);
printf("\n samoe dlinnoe slovo \n",string);
p1=strtok(string,geps);
while (p1!=NULL)
{
max_dlinna=strlen(p1);
p2=p1;
}
p1=strtok(NULL,geps);
}
printf("%s",p2);
getch();
}Решение задачи: «Найти самое длинное слово в тексте»
textual
Листинг программы
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char string[]="The first sentence. Sentenceaaaaaaa. The second sentence.";
char geps[]=" .";
char *b, *e, *p, *pmax_sentence = NULL, *pmax_word = NULL;
int l, max_dlinna = 0;
printf("String is '%s'.\n", string);
// max sentence
max_dlinna = 0;
for (b=string;;) {
for (;*b && *b == ' '; ++b);
e = strchr(b, '.');
if (e) 0[e]=0;
if (max_dlinna < (l = strlen(b))) {
max_dlinna = l;
if (pmax_sentence) free(pmax_sentence);
pmax_sentence = strdup(b);
}
if (!e) break;
0[e] = '.';
b = e+1;
}
// max word
max_dlinna = 0;
for (p=strtok(string,geps); p; p=strtok(NULL,geps)) {
if (max_dlinna < (l=strlen(p))) {
if (pmax_word) free(pmax_word);
pmax_word = strdup(p);
max_dlinna = l;
}
}
printf("The longest word is '%s'.\n", pmax_word);
printf("The longest sentence is '%s'.\n", pmax_sentence);
free(pmax_word);
free(pmax_sentence);
system("pause");
}