Find Length Of Longest Word In A String | C Programming Example

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

This is a very good tutorial. Thank you.
It works fine as written, but I'm having trouble figuring out how to get it to accept input.
The auto-grader I'm using provides a random text, and a simple scanf("%s", s) only gives the value for the first word. I've tried using getc(), and fgets(), but I can't figure out what arguments they're asking for.
If you, or someone else could point me in the right direction, I'd appreciate it.

Thank you for your time.

marcjohnson
Автор

Explanation was very clear...🙌
Can you please also tell how to print that word?

arunezio
Автор

hello i love this video! i have an assignement "Enter a string and calculate the length of the longest ascending fragment inside it. By ascending fragments, we mean that each letter in the sequence is arranged in ascending order. For example, if you enter a string frscrndsvnyrsgtdy, then there are 8 ascending segments in the string, namely frs, cr, n, dsv, ny, rs, gt and dy. obviously, the longest ascending segment is frs, which has a length of 3". Is this video simiar concept to this homework, would i be able to use it as a reference to solve this assignement ?

corniflower
Автор

Good evening. PLEASE! i don't understand why when i call the function in the main, it returns -1

vinkemtechnologies
Автор

Nice :
// An alternative method :

int longest_word(char *string){
if(!string || strlen(string) == 0) return 0;
int longest_word = 0;
int current_word = 0;
bool is_word = false;
char *delimiter = ", .\n\t";
for(int i = 0; string[i]; i++){
int j;
for(j = 0; delimiter[j]; j++){
if(delimiter[j] == string[i]){
break;
}
}
if(j == strlen(delimiter)){
if(!is_word){
is_word = true;
}
++current_word;
}
else {
if(is_word){
longest_word = longest_word > current_word ? longest_word : current_word;
is_word = false;
current_word = 0;
}

}
}
return longest_word;
}

justcurious