This is my first time to use the string function.
And I met some errors while doing the practice.

We now need to input some words, until we input a word with 4 letters.
Then the program will show the smallest and the biggest word among our inputs.

e.g.

Enter the word: dog
Enter the word: zebra
Enter the word: rabbit
Enter the word: cat
Enter the word: fish

Smallest word: cat
Biggest word: zebra

Here is my code. It seems have a fatel error.

#include<stdio.h>
#include<string.h>
#define N 100

void main()
{
	char smallest[N]={0L},biggest[N]={0L};
	char input[N];
	int i,length;
	do
	{
		printf("Enter the word: ");
		for (i=0;i<N;i++)
		{
			scanf("%c",&input[i]);
			if (input[i]=='\n')
				break;
		}
		printf("\n\n");
		length=strlen(input);
		if (strcmp(smallest,input)>=0)
			strcpy(smallest,input);
		if (strcmp(biggest,input)<0)
			strcpy(biggest,input);
	}
	while (length!=4);

	printf("\nThe smallest word is %c",smallest);
	printf("\nThe biggest word is %c",biggest);
}

Thanks:)

Recommended Answers

All 4 Replies

This looks more like c code and not c++. If you want to code this in c++ I would suggest using the string class in the stl. What error is your compiler giving you or does it crash while running?

I leart this style in the lecture.
I don't know it is C code or not...

Actually, it can pass the complier.
However, after I input the first word,
the program stops automatically and an error box jumps out.

So you don't think the error itself is worth knowing, just that the box itself jumps out. You probably go to the store and buy unlabled cans of food, too.

Yes, you are right.
So the problems should be the string function,and the logic also be wrong.
Since the crash appears while running that parts.
I think this part is the culprit.But I don't know what's wrong of it.

if (strcmp(smallest,input)>=0)
strcpy(smallest,input);
if (strcmp(biggest,input)<0)
strcpy(biggest,input);
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.