小小字符串函数

那个天真的人 2008-07-18 08:12:35
小小字符串函数!
编写一个函数,接受一个有大写和小写字母的串,返回这个串的全部是小写字母的备份,我写了下面这个
#include <iostream>
#include <string>
#include <stdlib>
using namespace std;
void Upper_Print( char *str1,char *str2)
{
int i=-1,j=0;
while(str1[++i]!='\0')
if(isupper(str1[i]))
str2[j++]=str1[i];
str2[j]='\0';
}
int main()
{
char * pt1=NULL,*pt2=NULL;
char str_array1[20],str_array2[20];
pt1=str_array1;
pt2=str_array2;
cout<<"input the str: ";
cin>>str_array1;
cout<<"the upper characters are : ";
Upper_Print(pt1,pt2);
cin.ignore(1);
cout<<pt2;
getchar();
return 0;
}

感觉很菜,谁帮忙写个更好点的,用string也行的。
...全文
147 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵Andy 2008-07-20
  • 打赏
  • 举报
回复
str2没有分配内存所以str2[j++]错了,对不起哩。
赵Andy 2008-07-20
  • 打赏
  • 举报
回复
对不起,7楼那个错了。
str2[j++]=str1[i];
这一句错了。
应该改为str2+=(str1[i]);
赵Andy 2008-07-19
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void Upper_Print(string str1)
{
int i=-1,j=0;
string str2;
while(str1[++i]!='\0')
if(islower(str1[i]))
str2[j++]=str1[i];
for(string::size_type ix=0;ix!=j;++ix)
cout<<str2[ix];
cout<<endl;
}
int main()
{
string str1,str2;
cout<<"input the str: ";
getline(cin,str1);
cout<<"the upper characters are : ";
Upper_Print(str1);
system("pause");
return 0;
}
见笑了。
那个天真的人 2008-07-19
  • 打赏
  • 举报
回复
因为不是写大程序来的,在练习写小程序所以才会写这个的,以后做程序会用到还是用一楼那个好多了,方便。
还有四楼那个程序
if(src[i]>0x40 && src[i]<0x60){
那个0x60改为0x5B就行了吧,这里des[i+1]=0;好像不对哦,是des[i]=0,因为那i在循环结束前已经加1了。
也谢谢二、三楼来光顾。
wyx8421 2008-07-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yanyan19880509 的回复:]
if(src[i]>0x40 && src[i] <0x60){
那个0x60改为0x5B就行了吧,这里des[i+1]=0;好像不对哦,是des[i]=0,因为那i在循环结束前已经加1了。
[/Quote]

引用的时候圈错了。。。。
wyx8421 2008-07-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yanyan19880509 的回复:]
不好意思哦,那个 if(isupper(str1[i])) 搞错了,应该是if(islower(str1[i]))的。
[/Quote]

恩恩~
LZ说得对,见笑了~
那个天真的人 2008-07-19
  • 打赏
  • 举报
回复
7楼那个行哦,呵呵
那个天真的人 2008-07-18
  • 打赏
  • 举报
回复
不好意思哦,那个 if(isupper(str1[i])) 搞错了,应该是if(islower(str1[i]))的。
wyx8421 2008-07-18
  • 打赏
  • 举报
回复
自己写了个简单的,见笑了~


#include <iostrem>
using namespace std;
const int LEN=20;
void main(void)
{
char src[LEN],des[LEN];
int i;
cin>>src;
for(i=0;src[i]!=0;i++){
if(src[i]>0x40 && src[i]<0x60){
des[i]=src[i]+0x20;
}else{
des[i]=src[i];
}
}
des[i+1]=0;
cout<<src<<endl;
cout<<des<<endl;
}
赵Andy 2008-07-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 yanyan19880509 的帖子:]
返回这个串的全部是小写字母的备份
[/Quote]
您的程序返回的是大写字母。
飞哥 2008-07-18
  • 打赏
  • 举报
回复
这个系统函数有实现了, 就不用那么费劲了

如果自己实现,就搞个数组(动态or静态)
将转换后的扔进去

如何转换?
大写和小写的值差多少你知道不?不知道就查查ascii码表。做做加减法的事
summersdw1 2008-07-18
  • 打赏
  • 举报
回复
字母大小写转换函数的声明包含在string.h头文件中,函数的原型如下:

char*strupr(char *string)

功能:将字符串string中的小写字母转换为大写,并返回指向string的指针。

char*strlwr(char *string)

功能:将字符串string中的大写字母转换为小写,并返回指向string的指针。

程序举例:

#include<iostream.h>

include<string.h>

//main()函数

void main(void)

{

//char string[80],*p;

int i;

//转换字符串中的小写字母为大写

cout<<"convert a string to uppercase:"<<endl;

cout<<"string:";

cin>>string;

p=strupr(string);

cout<<"p:"<<p<<endl;

cout<<"string:"<<string<<endl;

cout<<"--------------"<<endl;

//转换字符串的大小写字母为小写

cont<<"convert a string to lowercase:"<<endl;

cout<<"string:";

cin>>string;

p=strlwrz(string);

cout<<"p:"<<p<<endl;

cout<<"string:"<<string<<endl;

}

将程序命名为p3_164.cpp.

测试运行:

convert a string to uppercase:

string:AaBb123CcDd <enter>回车

p:AABB123CCDD

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧