char str[] = "zhengzhou is a big city";
int maxLength = 0;//存储最长单词长度.
int length = 0; //用来记录单词的长度.
int maxIndex = 0;//记录最长单词开始的下标
//使用while循环对字符串进行遍历.
int i = 0;
while (str[i] !='\0') {
if (str[i] != ' ') {
<
这段代码展示了一个在C语言中查找已知字符串中最长单词的方法。通过遍历字符串,遇到空格时比较当前单词长度与最长长度,更新最长单词信息。最后输出最长单词及其长度。
char str[] = "zhengzhou is a big city";
int maxLength = 0;//存储最长单词长度.
int length = 0; //用来记录单词的长度.
int maxIndex = 0;//记录最长单词开始的下标
//使用while循环对字符串进行遍历.
int i = 0;
while (str[i] !='\0') {
if (str[i] != ' ') {
<
被折叠的 条评论
为什么被折叠?