美文网首页
2019-08-29

2019-08-29

作者: 1墨家巨子 | 来源:发表于2019-08-29 16:20 被阅读0次
    /***************************************
     * 查找字符串中最长的单词,                *
     * 并打印输出最长的单词.                  *
     * char arr[]="I am apple position am";*
     * test_arr(arr);                      *
    ****************************************/
    int test_arr(char *arr){
        if(NULL==arr){
            return -1;
        }
        char *temp_arr=arr;
        int count=0;   //记单词个数
        int len=0;     //记指针的步长
        int temp=0;
        int max=0;     //记最长单词个数
        int position=0;//记单词首字母的位置
       while(len<=strlen(arr)){
            if(*temp_arr==' '||*temp_arr=='\0'){
              temp=count;
              if(max<temp){
                  max=temp;
                 position=len-max;
              }
              count=-1;
            }
            *temp_arr++;
             count++;
             len++;
        }
       int i;
       for(i=position;i<max+position;i++){
        printf("%c",arr[i]);
       }
     return 0;
    }

    相关文章

      网友评论

          本文标题:2019-08-29

          本文链接:https://www.haomeiwen.com/subject/lmniectx.html