您的位置:首页 > 编程语言 > C语言/C++

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

2013-04-25 22:32 417 查看
/***************************************************************************
功能:将输入的数据中的开头,结束的空字符去掉,并将大写字符转换成小写
用途:用于规范输入
****************************************************************************/
#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.

********************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐