字符串处理笔记

类似于strlen,strcmp,strpcy就不写了。。。。。。以下都是在VC6.0下编译,可能在VS上编译错误。

append()函数:

功能:C++一个字符串连接在另一个字符串后面。

代码:

#include<iostream> #include<string> using namespace std; void main() { string a="hello "; string b="world"; a.append(b,0,sizeof(b)+2); cout<<a<<endl; }


strchr()函数:

功能:C++从某一个字符串开始输出字符串。

代码:

#include<iostream.h> #include<string.h> void main() { cout<<strchr("hello world!",'e')<<endl; } 或者:

#include<iostream.h> #include<string.h> void main() { char *a="hello world!"; char b='e'; cout<<strchr(a,b)<<endl; }


strstr函数:

功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。

代码:

#include <stdio.h> #include <string.h> int main() { char str1[1001],str2[1001]; while(~scanf("%s %s",str1,str2)) { if(strstr(str1,str2)==0) { printf("No\n"); } else { printf("Yes\n"); } } return 0; }

strrev函数:

功能:把字符串s的所有字符的顺序颠倒过来(不包括空字符NULL)。

#include<stdio.h> #include<string.h> int main() { char str[1001]; while(~scanf("%s",str)) { strrev(str); printf("%s\n",str); } return 0; }

atoi函数:

功 能: 把字符串转换成整型数,如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。

代码:

#include<iostream.h> #include<stdlib.h> #define maxn 1001 int main() { char str[maxn]; while(cin>>str) { int n=atoi(str); cout<<"n="<<n<<endl; } return 0; }

substr函数:

功能:从一个字符串复制一个从指定位置开始,并具有指定长度的子字符串。

代码:

#include <iostream> #include <string> using namespace std; int main() { string s = "hello world!"; string str = s.substr(3,6); cout<<str<<endl; return 0; }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值