串的处理 蓝桥杯(代码)

Problem Description


在实际的开发工作中,对字符串的处理是最常见的编程任务。本题目即是要求程序对用户输入的串进行处理。具体规则如下:
1.  把每个单词的首字母变为大写。
2.  把数字与字母之间用下划线字符(_)分开,使得更清晰
3.  把单词中间有多个空格的调整为1个空格。

Input

我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。每个单词间由1个或多个空格分隔。假设用户输入的串长度不超过200个字符。

Output


请按照题意进行输出。

Sample Input

you and     me  what  cpp2005program
this is     a      99cat

Sample Output

You And Me What Cpp_2005_program
This Is A 99_cat



代码如下:

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main ()
{
    char str[220];
    int i,j,len;
    while(gets(str)!=NULL){
        len=strlen(str);
        for(i=0;i<len;i++){
            if(isalpha(str[i])){
                if(i==0||str[i-1]==' ')
                    str[i]=toupper(str[i]);

            }
            if(i>0&&(isalpha(str[i])&&isdigit(str[i-1])||isdigit(str[i])&&isalpha(str[i-1]))){
                printf("_");
            }
            if(i>0&&isalnum(str[i])&&str[i-1]==' '){
                printf(" ");
            }
            if(isalnum(str[i]))
                printf("%c",str[i]);
        }
        printf("
");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/lanaiwanqi/p/10445757.html