[computer science] c programming

sehj
LongWord.c

#include <stdio.h> #include <string.h> const int MAX = 1000; void getLongestWord(const char line[], char longest[]) { int longestLength = 0; // store the current word here char current[MAX]; // to store the effective length of current word, removing the quotes, hyphen etc int currentLength = 0; // total letters in the current word int currentCount = 0; int pos = 0; char ch; // calculate the length int len = strlen(line); while (pos < len) { // get the current character ch = line[pos]; // check if line ends here if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') { // if this is longest, copy the string if (currentLength > longestLength) { current[currentCount] = '\0'; strcpy(longest, current); longestLength = currentLength; } currentLength = 0; currentCount = 0; } else if ((ch == '\'' || ch == '-') && pos > 0 && isalpha(line[pos - 1]) && pos < len - 1 && isalpha(line[pos + 1])) { current[currentCount++] = ch; } else { current[currentCount++] = ch; currentLength++; } pos++; } if (currentLength > longestLength) { current[currentCount] = '\0'; strcpy(longest, current); } } int main(void) { char str[2*MAX]; char longest[MAX]; // get user input printf("Enter String:\n"); gets(str); // call function getLongestWord(str, longest); printf("the longest Word is: %s\n", longest); return 0; }