寻找一个字符串中的最长的单词

#include <stdio.h>
#include <string.h>
int main(){
    int find_longest_word(char str[]);
    char str[100];
    printf("Please input a sentence, we will find the longest word of the sentence.\n");
    gets(str);
    int begin=find_longest_word(str);
    printf("The longest word of the sentence is :");
    while((str[begin]>'a'&&str[begin]<'z')||(str[begin]>'A'&&str[begin]<'Z')){
        printf("%c",str[begin]);
        begin++;
    }
}

int find_longest_word(char str[]){
    int current_length=0;//define the length of current word(定义当前的单词的长度)
    int length=0;//define the length of the current longest word(定义当前遇到的最长的单词的长度)
    int flag=1;//define the new word is begin(定义出现新的单词的标志)
    int current_place;//define the first position of current longest word(定义当前的单词的第一个字母的位置)
    int place;//define the first position of the longest word(定义当前的字符串的最长的单词的第一个字母的位置)
    for(int i=0;i<=strlen(str);i++){
        if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
            if(flag){//if the new word appear(当新的单词出现的时候)
                current_place=i;//record the current word position(记录下新的单词的位置)
                flag=0;//next is not a new word, so we should reverse flag.(接下来的就不是新的单词了,因此对flag取反)
            }else{
                current_length++;//increase the length of the word.(累加当前的单词的长度)
            }
        }else{
            flag=1;//the new word will appear, so we should reverse the flag.(接下来可能会出现新的单词,因此对flag取反)
            if(current_length>length){
            //if the length of the new word is longer than the previous word.(如果新的单词的长度比之前的单词的长度要长)
                length=current_length;//refresh the new length(更新最长的单词的长度)
                place=current_place;//refresh the position of the new word(更新最长的单词的位置信息)
                current_length=0;//empty the length of the word, because the new word is start.(新的单词开始了,清空单词长度)
            }
        }
    }
    return place;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 答案:可以使用以下自定义函数来实现:def find_longest_word(str): words = str.split() max_len = 0 longest_word = "" for word in words: if len(word) > max_len: max_len = len(word) longest_word = word return longest_word print(find_longest_word(str)) ### 回答2: 首先,我们可以创建一个自定义函数`longest_word`来实现寻找最长单词的功能。函数的输入为一个字符串,返回值为最长单词。 在函数内部,我们可以使用`split()`方法将字符串分割成一个单词列表。然后,我们遍历这个单词列表,记录最长单词和其长度。 最后,返回最长单词即可。 下面是一个例子: ```python def longest_word(string): words = string.split() longest = "" length = 0 for word in words: if len(word) > length: longest = word length = len(word) return longest ``` 然后,我们可以在主程序获取用户输入的字符串,并将其传递给`longest_word`函数。 ```python input_string = input("请输入一个字符串:") result = longest_word(input_string) print("最长单词是:", result) ``` 通过以上的代码,我们输入一个字符串后,会打印出输入字符串最长单词。 ### 回答3: 题目要求输出一个字符串最长单词。我们可以按照以下步骤来解决这个问题。 步骤一:编写一个自定义函数来判断一个字符是否是字母。 ```python def is_letter(char): if 'a' <= char <= 'z' or 'A' <= char <= 'Z': return True else: return False ``` 步骤二:编写一个自定义函数来获取字符串最长单词。 ```python def longest_word(string): longest = "" current_word = "" for char in string: if is_letter(char): current_word += char elif current_word: if len(current_word) > len(longest): longest = current_word current_word = "" if len(current_word) > len(longest): longest = current_word return longest ``` 步骤三:调用自定义函数来获取字符串最长单词并输出结果。 ```python string = input("请输入一个字符串:") result = longest_word(string) print("最长单词是:" + result) ``` 以上是一个使用自定义函数来输出一个字符串最长单词的解决方案。这个方案可以在输入任意字符串的情况下,按照题目要求返回最长单词
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值