zoukankan      html  css  js  c++  java
  • 华为2013校园招聘上机笔试题 ---2 字符串处理转换

    2 字符串处理转换
    问题描述:    
    在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
    要求实现函数:
    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[]=""

    1 #include <iostream>
    2 #include <string.h> 3 #define BOOL int 4 #define TRUE 1 5 #define FALSE 0 6 using namespace std; 7 8 BOOL isLetter(char letter) 9 { 10 if(letter>='a'&&letter<='z'||letter>='A'&&letter<='Z') 11 return TRUE; 12 else 13 return FALSE; 14 } 15 16 char word[100][100]; 17 18 void main() 19 { 20 char input[]="%Abort^123 t 3453i*()" ; 21 char output[100]; 22 int i=0,j=0; 23 int flag=0; 24 int num=0; 25 for(int k=0;k<=(int)strlen(input);k++) 26 { 27 if(isLetter(input[k])) 28 { 29 word[i][j]=input[k]; 30 j++; 31 flag=1; 32 } 33 else 34 { 35 if(flag==1&&j==1) 36 { 37 j=0; 38 } 39 if(flag==1&&j>1) 40 { 41 word[i][++j]=''; 42 for(int m=0;m<i-1;m++) 43 { 44 if(strcmp(word[m],word[i])==0) 45 { 46 j=0; 47 flag=0; 48 break; 49 } 50 } 51 if(flag==0)continue; 52 i++; 53 j=0; 54 flag=0; 55 num++; 56 } 57 } 58 } 59 60 if(num==0) strcpy_s(output,""); 61 else 62 { 63 strcpy_s(output,word[num-1]); 64 for(int k=num-2;k>=0;k--) 65 { 66 strcat_s(output," "); 67 strcat_s(output,word[k]); 68 } 69 } 70 71 cout<<output<<endl; 72 getchar(); 73 74 }
  • 相关阅读:
    交通运输线(LCA)
    POJ 2186 Popular cows(Kosaraju+强联通分量模板)
    Codeforces 351D Jeff and Removing Periods(莫队+区间等差数列更新)
    Codeforces 375D
    Codeforces 86D
    SPOJ D-query(莫队算法模板)
    Educational Codeforces Round 25 D
    Codeforces Round #423 Div. 2 C-String Reconstruction(思维)
    HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)
    bzoj 2463: [中山市选2009]谁能赢呢?
  • 原文地址:https://www.cnblogs.com/593213556wuyubao/p/3399763.html
Copyright © 2011-2022 走看看