2019-08-29

2019-08-29  本文已影响0人  1墨家巨子
/***************************************
 * 查找字符串中最长的单词,                *
 * 并打印输出最长的单词.                  *
 * 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;
}
上一篇下一篇

猜你喜欢

热点阅读