字符串处理转换
问题描述:
在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
要求实现函数:
void my_word(charinput[], char output[])
【输入】 char input[], 输入的字符串
【输出】 char output[],输出的字符串
【返回】 无
示例
输入:charinput[]="some local buses, some1234123drivers" ,
输出:charoutput[]="drivers local buses some"
输入:charinput[]="%A^123 t 3453i*()" ,
别人的版本
问题描述:
在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
要求实现函数:
void my_word(charinput[], char output[])
【输入】 char input[], 输入的字符串
【输出】 char output[],输出的字符串
【返回】 无
示例
输入:charinput[]="some local buses, some1234123drivers" ,
输出:charoutput[]="drivers local buses some"
输入:charinput[]="%A^123 t 3453i*()" ,
输出:charoutput[]=""
下面是我自己写的,可能有点繁琐了
<pre name="code" class="cpp">#include<iostream>
using namespace std;
#define MAX 1000
#include<map>
bool isLetter(char letter)
{
if(letter>='a'&&letter<='z'||letter>='A'&&letter<='Z')
return true;
else
return false;
}
int main()
{
char str[MAX]={'\0'};
while(gets(str))
{//将单词放到Word中
char Word[MAX][20] = {'\0'}; //二维数组,每一行放一个单词
char *p = str;
int count = 0;
int i = 0;
while(*p)
{
while((*p>='A'&&*p<='Z')||(*p>='a'&&*p<='z'))
{
*(Word[count]+i) = *p;
i++;
p++;
}
count++;
i = 0;
while(*p&&(!((*p>='A'&&*p<='Z')||(*p>='a'&&*p<='z'))))
{
p++;
}
}
//所有的字符串都已保存
//按长度降序排序
if(count == 0)
{
cout<<""<<endl;
return 0;
}
//char **Word2 = new char*[count] ; //定义一个指针数组指向这个字符二维数组
//初始化这个指针数组
//for(int j = 0;j<count;j++)
//Word2[j] = Word[j];
char temp[20]={'\0'};
//按字符串长度排序,选一种稳定的排序算法
for(int j = 0;j<count;j++)
for(int k =1;k<count;k++)
{
if(strlen(Word[k-1])<strlen(Word[k]))
{
strcpy(temp,Word[k-1]);
//Word2[k-1] = Word2[k];
//Word2[k] = temp;
strcpy(Word[k-1],Word[k]);
strcpy(Word[k],temp);
}
}
//删除相同的单词
for(int j = 0;j<count;j++)
for(int i = j+1;i<count;i++)
{
if(strcmp(Word[i],Word[j])==0)
strcpy(Word[i],"\0");
}
//输出
for(int j = 0;j<count;j++)
{
if(strlen(Word[j])>1)
{
cout<<Word[j]<<" ";
}
}
cout<<endl;
}
system("pause");
return 0;
}
别人的版本
#include<iostream>
using namespace std;
#define MAX 1000
bool isLetter(char letter)
{
if(letter>='a'&&letter<='z'||letter>='A'&&letter<='Z')
return true;
else
return false;
}
char word[100][100]={'\0'};
int main()
{
char input[MAX]={'\0'};
char output[MAX]={'\0'};
gets(input);
int i = 0,j = 0;
int flag = 0;
int num = 0; //单词的数目
for(int k = 0;k<=strlen(input);k++)
{
if(isLetter(input[k]))
{
word[i][j] = input[k];
j++;
flag = 1;
}
else
{
if(flag == 1 &&1 == j)
{
j = 0; //长度为1的字符串
}
if(flag == 1 && j>1)
{
word[i][j]='\0';
for(int m = 0;m<i-1;m++)
{
if(strcmp(word[m],word[i])==0)
{
j = 0;
flag = 0;
strcpy_s(word[i],"");
break;
}
}
if(flag == 0)
continue;
i++;
j = 0;
flag = 0;
num++;
}
}
}
if(num == 0)
strcpy_s(output,"");
else
{
//排序操作
char temp[20]={'\0'};
for(int i = 0;i<num;i++)
for(int j = 1;j<num-i;j++)
{
if(strlen(word[j])>strlen(word[j-1]))
{
strcpy(temp,word[j]);
strcpy(word[j],word[j-1]);
strcpy(word[j-1],temp);
}
}
strcpy_s(output,word[0]);
for(int k = 1;k<num;k++)
{
strcat_s(output," ");
strcat_s(output,word[k]);
}
}
cout<<output<<endl;
system("pause");
return 0;
}