I can not solve this problem. I should write a code for PROGRAM in C, which finds the longest word of the text enter by the keyboard.

Plaese help me!

post your code here. We'll help you to solve your problem.

If want sm help to start up ..
use a counter and start counting with the first word, when SPACE detected word ends so temperoraly tht word has longest length store that length in a variable thn start counting length for next word, Compare word length for both.
Depending on the result store the MAX length and corresponding word .. Repeat for all words...
So Get started .. and post your code for any help

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
char sentence[200];
gets(sentence);
//How to write the code to find the word with the biggest length ?

system("PAUSE");
return 0;
}

Embedded your code in code tag..
U did nothing to solve your problem dude..
Try something if got problem in implementing your code we will help you

Use a loop, compare each character with space and increment a variable "temp_count" each time a character other than space is encountered. when space is met, compare "temp_count" with another variable "count". if temp_count is greater than count, assign count = temp_count and reset temp to zero to check other words. In end, count will have the value of the length of the longest word.
This should be enough.
A word of advice, try coding on your own. Its now or never; you gotta start working.

Ya ..
But as you need longest word dont forget to go on storing longest word along with the length ...

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    char a[50],b[20],c[20];
    int i,j=0,l=0;
    clrscr();
    printf("Enter a string:\n");
    gets(a);
    for(i=0;i<=strlen(a);i++)
    {
        if(a[i]!=32 && a[i]!='\0')
        b[j++]=a[i];
        else
        {
            b[j]='\0';
            if(strlen(b)>l)
            {
                strcpy(c,b);
                l=strlen(b);
            }
            j=0;
        }
    }
    printf("The longest word is:");
    puts(c);
    getch();
}
commented: No CODE Tags, terrible non-standard code, very old compiler -- basically a useful post for how not to code. -3

i couldn't understand the 17th line of the code.please help me

Member Avatar for iamthwee

It would best to ignore that non-standard piece of crap above and consider doing this yourself.

Line 17 puts the needed end of string marker char, into place. Remember, chars are just a bunch of chars, UNTIL they have an end of string marker in place, after them.

bunch of chars: just a bunch of chars.
now a string: just one string.'\0'

It's confusing, because the end of string marker is not seen when the string is printed. It's ONLY a marker, not a part of the string.

As is, the code above is non-standard, because it uses conio.h for getch(), and clrscr(). Neither should be necessary or desirable. (used here, because the poster was stuck with the very old Turbo C compiler).

It also has a bug, since punctuation is not accounted for in the entered string.

it's inefficient, because it requires that strlen() repeats itself, over and over for the same word. Although this is just a beginner study program, that's a consideration, still.

Overall, it's a good starting point for a program, but needs more work.

A square picture is cut into 16 squares and they are shuffled. Write a program to rearrange the 16 squares to get the original big square. I coulnt write this program can anyone help me with this program?

Member Avatar for iamthwee

Spammer? Robot?

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char str[100],word[50][50];
    int i=0,j=0,c=0,r=0,length=0,max=0,index[10];
    clrscr();
    printf("\n Enter any string: \n");
    gets(str);
    length=strlen(str);
    for(i=0;i<length;i++)
    {
        if(str[i]!=' ')
        {
            word[r][c]=str[i];
            c++;
        }
        else
        {
            word[r][c]='\0';
            r++;
            c=0;
        }
    }
    word[r][c]='\0';
    for(j=0;j<=r;j++)
    {
        if(strlen(word[j])>max)
        {
            c=0;
            max=strlen(word[j]);
            index[c]=j;
            c++;
        }
        else if(strlen(word[j])==max)
        {
            index[c]=j;
            c++;
        }
    }
    if(c==1)
    {
        printf("\n The largest word is: ");
        puts(word[index[0]]);
    }
    else
    {
        printf("\n The largest words are: \n");
        for(i=0;i<c;i++)
            puts(word[index[i]]);
    }
    getch();
}

The trick with this program is defining what's meant by a "word". You can't just take whatever is separated by whitespace and call it good, because that will incorrectly include punctuation.

To properly do it would require some lookahead and lookbehind, and even then poorly formatted sentences could cause an issue. So to a certain extent the program would need to be tailored to the expected input rather than act as a universal sentence recognizer.

Once you have reasonable tokenization logic, it's straightforward to do the deed:

#include <ctype.h>
#include <stdio.h>

int is_word_char(char c)
{
    return isalnum(c) || c == '\'' || c == '-';
}

const char *next_word(const char *s, size_t *len)
{
    *len = 0;

    // Find the next word
    while (*s != '\0' && !is_word_char(*s))
    {
        ++s;
    }

    // Find the end of the word
    while (s[*len] != '\0' && is_word_char(s[*len]))
    {
        ++(*len);
    }

    return *s != '\0' ? s : NULL;
}

int main(void)
{
    char line[BUFSIZ];

    printf("Enter a sentence: ");
    fflush(stdout);

    if (fgets(line, sizeof line, stdin) != NULL)
    {
        const char *p = line, *longest_word;
        size_t len, longest_len = 0;

        while ((p = next_word(p, &len)) != NULL)
        {
            if (len > longest_len)
            {
                longest_word = p;
                longest_len = len;
            }

            p += len;
        }

        printf("Longest word: %.*s\n", longest_len, longest_word);
    }

    return 0;
}

please put a simple program which can be understood by first year people please.......

please help me now someone i need it in urgent please..........

please put a simple program which can be understood by first year people please.......

My sample program meets that requirement.

#include <iostream>
using namespace std;

class MyString
{
    public:
            MyString();
            MyString(const char *message);
            MyString(const MyString &source);
            ~MyString();
            const void Print() const;
            const int Length() const;
            MyString& operator()(const int index, const char b);
            char& operator()(const int i);

            MyString& operator=(const MyString& rhs);
            bool operator==(const MyString& other) const;
            bool operator!=(const MyString& other) const;
            const MyString operator+(const MyString& rhs) const;
            MyString& operator+=(const MyString& rhs);
            friend ostream& operator<<(ostream& output, const MyString& rhs);
            const int Find(const MyString& other);
            MyString Substring(int start, int length);

    private:
            char *String;
            int Size;


   };

  istream& operator>>(istream& input, MyString& rhs);

Just try out this code. Hope it may be useful for you.

#include<stdio.h>
#include<conio.h>
void main()
{
int w=0,i=0,j=0,l,a[10],big;
char ch[25],st[10][20];
clrscr();
printf("Enter the string:");
gets(ch);
while(ch[i]!='\0')
{
if(ch[i]==' ')
{
st[w][j]='\0';
a[w]=j;
j=0;
w++;
}
else
{
st[w][j]=ch[i];
i++;
}
i++;
}
big=l=0;
for(i=0;i<w;i++)
{
if(big<a[i])
{
big=a[i];
l=i;
}
}
printf("The longest word is %s",st[1]);
getch();
}

Just try out this code. Hope it may be useful for you.

That code is pretty awful, even after adding reasonable indentation. Let's hit the low hanging fruit first.

#include<conio.h>

This is a good indicator of bad code, because it's usually unnecessary. Further look at the code confirms that this is the case for your code. All you've done is hurt portability by requiring a non-portable library when it's not needed.

void main()

This is also non-portable. If the compiler supports it, okay. If not, you've invoked undefined behavior. Not a good trade off. Especially when the code can be made correct across the board by using int main() and returning 0.

int w = 0, i = 0, j = 0, l, a[10], big;
char ch[25], st[10][20];

Variable names suck and are uninformative. The code is much harder to read as a result.

clrscr();

Unneeded, non-portable, antisocial, and anyone who does this should be ashamed. Put simply, if you create a console program that doesn't explicitly own the console and clears the screen, you'll piss off a lot of users who find the results of previous programs suddenly erased. Or maybe not, because nobody will want to use your program.

printf("Enter the string:");

stdout isn't flushed, so there's no guarantee this prompt will be visible before blocking for input. Unless you print a newline in the prompt, it's a good idea to call fflush.

printf("Enter the string:");
fflush(stdout);

gets(ch);

Never use gets. Ever. It's unsafe and there's no way to make it safe. fgets is the alternative:

fgets(ch, sizeof ch, stdin);

You should also check the result of any request for input and handle errors.

printf("The longest word is %s", st[1]);

No newline and no flush.

getch();

Non-portable and unnecessary.

Now for the more logic related stuff. Your algorithm doesn't work for a number of reasons.

  • You increment i one too many times in the while loop, so you're skipping characters in the string.

  • You fail to increment j at all which means each word will be at most 1 character long.

  • The last word is also not properly terminated with '\0', nor is its size added to a.

  • Oh the irony, you mixed up l and 1 when printing the longest word, thus providing a reason for using informative variable names all on your own.

  • The algorithm only allows 10 words, which is prohibitive. A better approach would be to use just a single working string and length as in my example where the number of supported words is essentially unlimited.

commented: "anyone who does this should be ashamed" ... little bit funny. :-) +4

Thanks a lot for your valuable comment. I could acquire many informative tips from your explanation. It is a great description that a beginner like to have.

deceptikon ,your code is kinda confusing. Could you please explain a bit <what> is going on in your code?

commented: Let's keep the language pleasant, shall we? -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.