The program is meant to read text input and simply print out the lengths of the 2 longest words and the actual words themselves. The words, you can assume are no longer than 100 characters. The problem I have is more to do with obtaining the actual string to process (I'm not really flash with strings and co. )
Heres what I've done so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXWORDLEN 100
int main(int argc, char *argv[])
{
char max_word[MAXWORDLEN], sec_word[MAXWORDLEN];
char current_word[MAXWORDLEN];
int max_length = 0, sec_length = 0;
int length = 0;
int input = 0;
printf("Input words here: \n");
do {
// ****** PROBLEM HERE **********
while (current_word = gets(stdin)){
// check if it contains a space, which means its a word
if (isspace(current_word) == 1)
{
// test for length
for (int i = 0, length = 0; current_word[i] != NULL; i++, length++)
;
// if biggest store in max_word[]
if (length > max_length)
{
sec_length = max_length;
max_length = length;
strcpy(max_word, current_word);
}
// test if second biggest, if yes store in sec_word[]
else if (length > sec_length)
{
sec_length = length;
strcpy(sec_word, current_word);
}
}
printf("Press 1 if you want to exit!\n");
scanf("%d",&input);
} while (input != 1); // terminates when input == 1
return 0;
}
I know that while() line is totally wrong haha, but how could I change it so that it reads a character and checks if the character is a space/tab or not (using isspace()) and then store it in the current_word[] string. If isspace() returns a 1, the rest of the code should work.. unless I missed out something small or big
Any suggestions would be greatly appreciated!