Find And Print The Longest Word In A String | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Great video! I have few suggestions on how we go through each word in the string and determine the longest one of them. This uses strtok function which skips to the nonwords character and counts

char str[] = "A sentence with several words"
char nonwords[] = " ., ?!\n\t";

int max=0;
char max_word[50];

char *ptr = strtok(str, nonwords);
while (ptr != NULL)
{
count = strlen(ptr);
if (count > max)
{
max = count;
strcpy(max_word, ptr);
max_word[strlen(max_word)] = '\0';
}
ptr = strtok(NULL, nonwords);
}

printf("\nLongest word: %s", max_word);

drig
Автор

If I don't use function then I don't have to create that *word right?

naruto
Автор

GREAT VIDEO...although i didn't get the line
temp_buffer[count]='\0'
won't that make the value of temp buffer \0?
sorry if this is a stupid question, i'm new to programming

saadatraja
Автор

Hello, is there a way to print multiple long words with the same length?

muhammadaushafalfarras
Автор

Please, can you write code for the smallest word??

Or what changes need to do in this code?

Скорпиончик-го