c++: 实现将字符串最短的单词输出,在主函数中输入字符串,编写一个函数完成最短单词的查找

来源:百度知道 编辑:UC知道 时间:2024/06/12 01:28:18
例如
输入单词串:happy new year.
最短单词 : new

给你个简单点的
#include <string.h>
#include <stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;

void main( void )
{
char string[255] ;
char seps[] = " ,\t\n";
char *token;
char strRes[255];
int strL = 255;
cin.getline(string,255);
token = strtok( string, seps );
while( token != NULL )
{
int tmpL = strlen(token);
if(tmpL <= strL)
{
strcpy(strRes,token);
}
token = strtok( NULL, seps );
}
printf("%s\n",strRes);
system("Pause");
}

字符串的最大长度自己根据实际情况定,我这里是255,我的方法主要用到了一个字符串的处理函数strtok(),这个函数的就是根据自己的需要,拆分字符串

//找出所有最短单词
#include <iostream>
#include <vector>
using namespace std;

int main()
{
char str[256];
int min = 256;
gets(str);
int i,k=0,len=0;
char c;
v