0

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:)

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by tc121091
0

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?

0

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.

0

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.

0

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);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.