题目:找到一串字符串中最长的单词,打印单词,并打印其长度和开始的索引下标
#pragma once#include<stdio.h>
#include<stdbool.h>
#include<ctype.h>
#include<string.h>void printfLongestWord(char* str) {int maxLength = 0;int currLength = 0;int startIndex = 0;bool isInWord = false;for (int i = 0; str[i] != '\0'; i++){if (!isspace(str[i])) {if (!isInWord) {startIndex = i;isInWord = true;}currLength++;}else{if (isInWord) {if (currLength > maxLength){maxLength = currLength;startIndex = i - maxLength;}isInWord = false;currLength = 0;}}}if (isInWord && currLength > maxLength) {maxLength = currLength;startIndex = strlen(str) - maxLength;}if (maxLength > 0){printf("最长单词为 :%.*s\t开始索引为:%d\t 单词长度为:%d", maxLength, str + startIndex, startIndex, maxLength);}else{printf("没有输入单词\n");}
}