C++中对字符操作的函数集锦

下面是要讲解的目录:

/*
1.字符转数字(atoi,atol,atof)
2.查找(strstr)
3.比较(strcmp)
4.复制(strcpy,memcpy)
5.连接两字符串(strcat)
6.逆置字符数组(strrev)
7.小写字母转大写(strupr)
*/

1.字符转数字(atoi, atol, atof)

#include<iostream>    
#include<cstdlib>  //头文件  
#include<string>    
using namespace std;
int main()
{
	//1.函数原型:int atoi (const char * str);  
	char ch1[10] = "123.2abc"; char ch2[10] = "abc123";
	//从当前地址开始往后查找,遇见非数字或'.'或'\0'停止工作  
	int a = atoi(ch1); int b = atoi(ch2);

	cout << a << endl;//123    当前地址往后为123,到'.'结束  
	cout << b << endl;//0      当前地址上字符为a,直接结束,返回0  
	//这里存在一个问题,如果当前本不是数字呢?直接结束,然后返回0,但是如果是这种  
        //情况呢。char ch[10]="0abc";它的结果也是返回0,如果你要使用if语句,这里是不严  
	//谨的,所以建议如果你使用atoi ,那么应该是在你知道这个字符数组当前位置即是数字  
	//的情况下。  

	string str = "a123bcef";//这里使用string  
	int c = atoi(&str[0] + 1);
	cout << c << endl;//123  


	//2.函数原型:long int atol ( const char * str )  
	//3.函数原型:double atof (const char* str)  
	//同上,不需多说  
	return 0;
}

2.查找(strstr)

#include<iostream>    
#include<cstring>  //头文件  
#include<string>    
using namespace std;
int main()
{
	/*函数原型:
	const char * strstr ( const char * str1, const char * str2 );
	      char * strstr(char * str1, const char * str2);
	*/
	char ch[10] = "abcdef";
	char *p, *pp;
	p = strstr(ch, "cd");
	pp = strstr(ch, "12");//找不到返回NULL  
	cout << p << endl;
	if (pp == NULL) { cout << "无法找到!\n"; }
	/*
	输出:
	cdef
	无法找到!
	*/

	/*
	p = strstr(ch, "a");//对单字符的查找
	cout << p;//abcdef
	*/
	return 0;
}

3.比较(strcmp)

#include<iostream>  
#include<string.h>//头文件  
using namespace std;
int main()
{
	//函数原型:int strcmp ( const char * str1, const char * str2 );  
	char a[10] = "abc";
	char b[10] = "ABC";
	char c[10] = "abc";
	if (strcmp(a, b)>0) cout << "a>b" << endl;//a>b  
	if (strcmp(b, a)<0) cout << "a<b" << endl;//a<b  
	if (strcmp(a, c) == 0) cout << "a==b" << endl;//a==b  
	return 0;
}

4.复制(strcpy, memcpy)

strcpy和memcpy都是标准C库函数,它们有下面的特点。
strcpy提供了字符串的复制。即strcpy只用于字符串复制,
并且它不仅复制字符串内容之外,还会复制字符串的结束符。

已知strcpy函数的原型是:char* strcpy(char* dest, const char* src);
memcpy提供了一般内存的复制。即memcpy对于需要复制的内容没有限制,因此用途更广。
我们来看函数原型就知道了:void * memcpy(void * destination, const void * source, size_t num);
关于void*代表什么意思,建议读者百度,关于它的博客很多,这里不作讲解。

strcpy和memcpy主要有以下3方面的区别。
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy

我们重点讲解memcpy,也推荐读者多多使用这个函数

#include <stdio.h>  
#include <string.h>  
int main()
{
	char str1[] = "Sample string";
	char str2[40];
	char str3[40];
	strcpy(str2, str1);
	strcpy(str3, "copy successful");
	printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);
	/*输出:
	str1: Sample string
	str2: Sample string
	str3: copy successful
	*/
	return 0;
}

下面来看memcpy函数的使用

#include<iostream>  
#include<string.h>//头文件  
using namespace std;
int main()
{
	//1.  
	char ch1[10] = "123456";
	char ch2[10];

	memcpy(ch2, ch1, sizeof(ch1));//注意最后的'\0',也复制进去  
	cout << ch2 << endl;//123456  

	//2.  
	int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int b[5];
	memcpy(b, a, sizeof(int) * 5);//这里只复制前5个  
	for (int i = 0; i<5; i++)
		cout << b[i] << " ";//1 2 3 4 5  

	return 0;
}

5.连接两字符串(strcat)

#include<iostream>
#include<string.h>//头文件
using namespace std;
int main()
{
	//函数原型:char * strcat ( char * destination, const char * source );
	char ch[10] = "123";
	char ch1[3] = "45";
	strcat(ch, ch1);//注意要保证ch有足够的空间存储
	cout << ch << endl;//12345
	return 0;
}

6.逆置字符数组(strrev)

#include<iostream>
#include<string.h>//头文件
using namespace std;
int main()
{
	//函数原型:char *strrev(char *s)
	char ch[7] = "abcdef";
	strrev(ch);
	cout << ch << endl;//fedcba
	return 0;
}

以下代码来自:  http://blog.csdn.net/turingo/article/details/8124432

strrev函数不常用,不过在进行数制转换和加密等场合还是有机会用到,因为一些针对嵌入式平台的编译器和VC对它提供了支持。对于不支持strrev函数的编译器,许多网友提供了不少很有价值的解决方案,不过直接从VC所提供的源代码中去提炼,似乎更能够兼顾效率和移植性,以下提供一份经典的实现代码:

[cpp]  view plain copy
  1. char* strrev(char* s)  
  2. {  
  3.     /* h指向s的头部 */  
  4.     char* h = s;      
  5.     char* t = s;  
  6.     char ch;  
  7.   
  8.     /* t指向s的尾部 */  
  9.     while(*t++){};  
  10.     t--;    /* 与t++抵消 */  
  11.     t--;    /* 回跳过结束符'\0' */  
  12.   
  13.     /* 当h和t未重合时,交换它们所指向的字符 */  
  14.     while(h < t)  
  15.     {  
  16.         ch = *h;  
  17.         *h++ = *t;    /* h向尾部移动 */  
  18.         *t-- = ch;    /* t向头部移动 */  
  19.     }  
  20.   
  21.     return(s);  
  22. }  

7.小写字母转大写(strupr)

#include<iostream>
#include<string.h>//头文件
using namespace std;
int main()
{
	char str[] = "abcdef";
	strupr(str);
	cout << str << endl;//ABCDEF
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值