码迷,mamicode.com
首页 > 编程语言 > 详细

Java程序员学习C++之字符串

时间:2015-07-21 12:49:16      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:c++

#include <iostream>
#include <string.h>
using namespace std;

int my_len(const char *str)
{
	int len = 0;
//	while (*str != '\0')
//	{
//		++len;
//		++str;
//	}

	while (*(str++) != '\0')
	{
		++len;
	}


	return len;
}

void my_cpy(char *dst,const char *src)
{
	while (*src != '\0')
	{
		*dst = *src;
		++dst;
		++src;
	}
}

int main()
{
	char str1[] = "abc";//后面自动加上'\0'
	char str2[] = { 'a', 'b', 'c' };//后面不会自动加上个'\0',作为字符串会一直找到'\0'标志
	char str3[] = { 'a', 'b', 'c' ,'\0'};
	char str4[10] = "abc";

	//              4                      3                      4                      10
	cout << sizeof(str1) << "," << sizeof(str2) << "," << sizeof(str3) << "," << sizeof(str4) << endl;
	//              3                      15                      3                      3
	cout << strlen(str1) << "," << strlen(str2) << "," << strlen(str3) << "," << strlen(str4) << endl;
	cout << my_len(str1) << "," << my_len(str2) << "," << my_len(str3) << "," << my_len(str4) << endl;

	//       abc       abc烫烫烫烫蘟bc    abc            abc
	cout << str1 << "," << str2 << "," << str3 << "," << str4 << endl;

	//字符串赋值
	//str1 = str2;// 错误,数组名是地址常量
	//str1 = "hello";// 错误,数组名是地址常量
	strcpy(str4,str1);

	//strcmp(str1,str2);字符串比较
	//strcat(str3,str2);字符串拼接,必须判断空间是否够

	//字符串分割
	char str[] = "this is a test";
	char *token = strtok(str, " ");
	while (token != NULL)
	{
		cout << token << endl;
		token = strtok(NULL," ");
	}

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Java程序员学习C++之字符串

标签:c++

原文地址:http://blog.csdn.net/easion_zms/article/details/46984115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!