串的处理

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

例如:
用户输入:
you and     me what  cpp2005program
则程序输出:
You And Me What Cpp_2005_program


*/
#include<stdio.h>
#include<string>
using namespace std;
//' '为空格字符    ''为空字符
void Upper(char * x)
{
 if(x[0]>=97 && x[0]<=122) x[0]=x[0]-32;
 for(int i=0;i<strlen(x);i++)
 {
 if(x[i]==' ' && x[i+1]>=97 && x[i+1]<=122)
   
       x[i+1]=x[i+1]-32;
 }
}
void deblank(char * x)
{
 char dst[200];
 int i=0,flag=0;
 int dst_index=0;
 while(x[i]!='')
 {
  if(x[i]!=' ')
  {
   flag=1;
   dst[dst_index++]=x[i];
  }
  else{
   if(flag)
   {
    dst[dst_index++]=' ';
    flag=0;
   }   
  }
  i++;
 }
  dst[dst_index]='';
  strcpy(x,dst); 
}

void line(char * x)
{

 char dst[200];
 int i=0,flag=0;
 int dst_index=0;
 while(x[i]!='')
 {  
 if(( x[i]>=97 && x[i]<=122)
   &&( x[i+1]>=48 && x[i+1]<=58)){
  // if(!flag)
  // { 
          dst[dst_index++]=x[i];
    dst[dst_index++]='_';
   // flag=0;
  // }   
  }
 else if(( x[i]>=48 && x[i]<=58)
   &&( x[i+1]>=97 && x[i+1]<=122))
 {
  dst[dst_index++]=x[i];
  dst[dst_index++]='_';
 // dst[dst_index++]=x[i];
 }

 else
 {
   //flag=1;
   dst[dst_index++]=x[i];
 }

  i++;
 }
  dst[dst_index]='';
  strcpy(x,dst); 

}
int main()
{
 char a[100];
 gets(a);
 
 Upper(a);

 deblank(a);

 line(a);

    puts(a);
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值