规范输入 :将输入的数据中的开头,结束的空字符去掉,并将大写字符转换成小写

/***************************************************************************
功能:将输入的数据中的开头,结束的空字符去掉,并将大写字符转换成小写
用途:用于规范输入
****************************************************************************/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctype.h>
#include<iostream>
using namespace std;
/**flag==1 大写转化为小写**/
/**flag==0 小写转化为大写**/
void transfStr(char *dest,bool flag)
{
  char *ptr;
  int len;

  ptr = dest;//将一个数组的首地址给它

  while (isspace(*ptr))//这里是用来去掉开头的空格
    ptr++;//只是改变了ptr的指向

  len = strlen(ptr);
  if (ptr > dest)//比较两个数组的指向
    memmove(dest, ptr, len+1);//将数据搬到了数据的开头

  ptr = dest+len-1;//prt指向dest的最后

  while (isspace(*ptr))//用于去掉最后的空格
    ptr--;

  *(ptr+1) = '\0';//最后一个字符加上字符串结束符

  ptr = dest;//让ptr指向数组最开头

    if(flag == 1)
        while (*ptr!='\0')//如果没有到字符串最后
        {
          *ptr = tolower(*ptr);//将大写字母转换成小写
          ptr++;
        }
    else
        while (*ptr!='\0')//如果没有到字符串最后
        {
          *ptr = toupper(*ptr);//将大写字母转换成小写
          ptr++;
        }
}
void test1()
{
    char buf[256]=
    "   To recap, the three main objectives in the Mystery Method are: \
\nTo attract a woman \
\nTo establish comfort, trust, and connection \
\nTo structure the opportunity to be seduced                      ";
    printf("*%s*\n",buf);
    transfStr(buf,1);
    printf("*%s*\n",buf);

}
void test2()
{
    char buf[256]=
    "   To recap, the three main objectives in the Mystery Method are: \
\nTo attract a woman \
\nTo establish comfort, trust, and connection \
\nTo structure the opportunity to be seduced                      ";
    printf("*%s*\n",buf);
    transfStr(buf,0);
    printf("*%s*\n",buf);

}
int main()
{
    test1();
    test2();
    char buf[80];
    printf("input:");
    fflush(stdout);
    fgets(buf,80,stdin);
    printf("*%s*\n",buf);
    transfStr(buf,1);
    printf("*%s*\n",buf);

    return 0;
}
/*******************
*   To recap, the three main objectives in the Mystery Method are:
To attract a woman
To establish comfort, trust, and connection
To structure the opportunity to be seduced                      *
*to recap, the three main objectives in the mystery method are:
to attract a woman
to establish comfort, trust, and connection
to structure the opportunity to be seduced*
*   To recap, the three main objectives in the Mystery Method are:
To attract a woman
To establish comfort, trust, and connection
To structure the opportunity to be seduced                      *
*TO RECAP, THE THREE MAIN OBJECTIVES IN THE MYSTERY METHOD ARE:
TO ATTRACT A WOMAN
TO ESTABLISH COMFORT, TRUST, AND CONNECTION
TO STRUCTURE THE OPPORTUNITY TO BE SEDUCED*
input:   to me TOMMORROW IS  a big day
*   to me TOMMORROW IS  a big day
*
*to me tommorrow is  a big day*

Process returned 0 (0x0)   execution time : 23.375 s
Press any key to continue.

********************/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值