hi everybody,
i want to write a program to print the longest word in a sentence but without using any built in function. can anybody please help me write this code in c language.

Yes, someone can help. What do you have so far? If you don't have anything, what's your thought process for working it out? Have you analyzed the structure of a sentence to break it down into words?

Note that "help" does not constitute doing work for you.

If you think about it for more than a few minutes you'll realise this is really simple... you just need a couple loops.
In psuedo code its something like:

wordStartIndex = 0
IndexOfLongestWordStart = 0
charCount = 0
count from wortStartIndex to space character until of string
    If currentIndex - (wordStartIndex + 1) > charCount
        IndexOfLongestWordStart = currentIndex
        charCount = currentindex - (wordStartIndex + 1)
    wordStartIndex = currentIndex + 1
count from IndexOfLongestWordStart to (IndexOfLongestWordStart + charCount)
    print char

Edited 2 Years Ago by cayman

ya i am able to break the sentence into words. i am also able to get the size of each word but i am unable to write the code for printing the longest word. if u want then i can give u the code how to break the sentence into each single word.

if u want then i can give u the code how to break the sentence into each single word.

Yes indeed. Showing what you already have is a good start.

i think i found the answer for this prog. i am posting the codes down. please comment if it is correct or not:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,max=0,count=0,j;
char str[100]; 
clrscr();
printf("\nEnter the string\n:");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(!(str[i]==' '))
{
count++;
}
else
{
if(max<count)
{
j=i-count;
max=count;
}
count=0;
}
}
if(max<count)
{ 
j=i-count; 
max=count; 
} 

for(i=j;i<(j+max);i++)
printf("%c",str[i]); 
getch(); 
}

At a glance, the logic looks okay. There are some nasty habits that should be eliminated though:

#include<conio.h>

The conio library is not needed in this program, it and all functions from it should be removed to maximize portability. This header is not standard, so you're basically limiting yourself to Turbo C variants.

void main()

This is not standard either. If the compiler supports it, you're fine. If the compiler doesn't support it, you've invoked undefined behavior. Given the risk, it's much better to simply use int main() and return 0 at the end.

gets(str);

Please forget that gets exists. In the latest standard it's been deprecated, and there's no way to make it safe. If you try to input a string with more than 99 characters, you're basically screwed. fgets is the recommended alternative.

I won't go into a rant about clrscr and getch, because they're both functions from the conio library and I've already recommended that they be removed. However, if you want, I can go into more detail about why they're bad in this case.

One problem with the logic is that it doesn't take punctuation into account. Let's say your sentence is "Hi fellow pickles, I'm a pickle too!!!!!!". You'd incorrectly say that "too!!!!!!" is the longest word, because bangs aren't excluded from the count.

Edited 2 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.