Misc string test

/*
 * std::string深入详解
 * Visual Studio 2008Sp1, 使用Ctrl + F5启动调试
 */
#include <iostream>
#include <string>
#include <cstdio>
#include <cstddef>
#include <cstring>
#include <cstdlib>   //qsort
#include <errno.h>		/* Error Codes */
#include <numeric>
#include <algorithm>
using namespace std;

//#define _CRT_SECURE_NO_WARNINGS with /D
#pragma warning(disable:4996)  //disable stupid warning of This function or variable may be unsafe. 
								//Consider using strerror_s instead

#define  MAXLEN 1000
char line[MAXLEN];

int getline(char s[], int lim)
{
	int ch = 0, i;
	
	i = 0;
	while(--lim > 0 && ((ch = getchar()) != EOF) && ch != '\n')
		s[i++] = ch;
	if (ch =='\n')
		s[i++] = ch;
	s[i] = '\0';

	return i;
}

//raise
int cmp(const void *a, const void *b)
{
	return *(char *)b - *(char *)a;
}

int main(int argc, char *argv[]){
	const basic_string<char> s1("test something");
	string s2("test");

	// The first member function  C-string
	string stra("hello ");
	const char *cstra = "c-string";
	stra.append(cstra);
	cout<<"Appending the C-string cstra to string stra gives: "<<stra<<endl<<endl;

	 The second member function
	const char *cstrb = "some elsd";
	stra.append(cstrb,2);
	cout<<stra<<endl<<endl;

	cout<<"input a character string"<<endl;
	if(getline(line, MAXLEN) < 1)
		exit(1);

	//quick sort
	cout<<"before line: "<<line<<endl;
	qsort(line, sizeof(line)/sizeof(line[0]), sizeof(line[0]), cmp);
	cout<<"quick sort line: "<<line<<endl;

	///string 测试
	cout<<"测试string ss,下面给出ss的字符串:"<<endl<<endl;
	string ss("    maybe you are long long girl, i'm who are you.12563.");
	cout<<ss<<endl<<endl;

	cout<<ss.find_first_not_of(' ')<<endl;     //前面有很多空格,查找第一个非空格的
	cout<<ss.find_first_not_of("abcdefghijkmno")<<endl;  //返回第一个不在指定字符集里面的元素位置

	cout<<ss.find_last_not_of("abcdefghijkmno")<<endl;

	cout<<ss.find("longk")<<endl;   //如果string中没有查找的内容,他会超出范围的去查找

	cout<<"计算string ss中的 o 个数:"<<count(ss.begin(), ss.end(), 'o')<<endl;
	cout<<"string ss的长度: "<<ss.size()<<",  string ss中的字母和数字: "<<count_if(ss.begin(), ss.end(), isalnum)<<endl;  //条件比较,ctype.h isalnum判断字母或者是数字


	//
	//strcspn && strspn
	cout<<endl<<endl<<endl;
	const char *pszTest = "long long ago, there is girl, she\'name is little redhat";
	cout<<"\r\n测试strcspn()函数,待测试的字符串pszTest: "<<pszTest<<endl;

	cout<<"长度:"<<strlen(pszTest)<<"---firt_not_of length `xyza` "<<strcspn(pszTest, "xyza")<<endl;

	//
	//test  strtok
	char str[] = "now#is the time for all#####good men to come to the#aid of their country\0";
	char *delims = "#";
	char *token = NULL;

	cout<<"\n\n测试strtok函数(linux下请使用函数·char *strsep (char ** __stringp, const char * __delim)·),"
		"待测试的字符串:\n"<<str<<endl<<endl;
	token = strtok(str, delims);	//线程不安全的函数, 列外str被破坏掉了
	while(token != NULL)
	{
		printf("%s\n", token);
		token = strtok(NULL, delims);
	}

	//
	//memchr
	cout<<"\n\n\n测试函数void * memchr (void * ptr, int value, size_t num );\r\n"<<endl;

	char *pch;
	int ch;

	ch = 'e';
	strcpy(str, "now#is the time for all#####good men to come to the#aid of their country" );
	cout<<"The test str:\n"<<str<<endl;
	pch = (char *)memchr(str, ch, strlen(str));
	if (pch != NULL){
		printf(">>Character \'%c\' is found at %d.\n", ch, pch - str +1);
		printf(">>%s\n", pch);
	}else{
		printf("Fuck, Character \'%c\' is NOT found!\n", ch);
	}

	cout<<"\n\n"<<endl;

	//
	//strerror
	cout<<"测试strerror(),创造一个error: press any key continue..."<<endl;
	cin.get();
	//for(int err = 1; err < 42; err++){
	//	printf("Error code%d: %s\n", err, strerror(err)); 
	//}
	FILE *file;
	file = fopen("unexist.file", "r");
	if(file == NULL)  
		printf("Open file crashed, Error code %d: %s\n", errno, strerror(errno));

	///
	//strcasecmp
	cout<<"\n\n测试strcasecmp()函数,输入一只动物吧!(dog,cat,etc...)"<<endl;
	
	/*使用自己的C函数*/
	if(getline(line, MAXLEN) >1){
		line[strlen(line) - 1] = '\0';	//delete new line

		/* do something compare */
		if (stricmp(line, "dog") == 0){  //stricmp实质是引用了string.h中的strcasecmp()函数,坑爹啊
			printf("Dog is very interesting....en?\n");
		}else if (stricmp(line, "cat") == 0){
			printf("Actually, I do not like cats.\n");
		}else if(stricmp(line, "cow") == 0){
			printf("Cattle (colloquially cows) are the most common type of large domesticated ungulates."
				   "They are a prominent modern member of the subfamily Bovinae, are the most widespread "
				   "species of the genus Bos, and are most commonly classified collectively as Bos primigenius."
				   " Cattle are raised as livestock for meat (beef and veal), as dairy animals for milk and "
				   "other dairy products, and as draft animals (oxen / bullocks) (pulling carts, plows and "
				   "the like). Other products include leather and dung for manure or fuel. In some countries, "
				   "such as India, cattle are sacred. From as few as eighty progenitors domesticated in "
				   "southeast Turkey about 10,500 years ago, it is estimated that there are now 1.3 billion "
				   "cattle in the world today.\n");
		}else if (stricmp(line, "pig") == 0){
			printf("A pig is any of the animals in the genus Sus, within the Suidae family of even-toed "
				"ungulates. Pigs include the domestic pig, its ancestor the wild boar, and several other "
				"wild relatives. Pigs are omnivores and are highly social and intelligent animals.\n");
		}else{
			printf("%s ,?what animal it was??\n", line);
		}
	}

	//使用istream对象, std::string convert to C-style string
	cout<<"输入一些什么东西吧\n"<<endl;
	std::string szline;
	std::getline(std::cin, szline);   //don't be std::cin
	strcpy(line, szline.c_str());
	printf(">>%s\n", line);


	///
	//下面看看一个typedef与const的结合
	typedef std::string *pstring;
	//const pstring mystring;		//error, 因为mystring变量是const类型的,先要初始化
	//const int jj;				//error

	std::string mystr1("got some string here.");
	std::string mystr2("wow,i got the second string.");
	const pstring mystring = &mystr1;
	//mystring = &mystr2;	//error 指针mystring只是指向mystr1的

	cout<<"mystring: \n"<<*mystring<<endl;

	mystring->append(" some append string.");

	cout<<"after append of mystring:\n"<<*mystring<<endl;


	return 0;
}


输出结果

Appending the C-string cstra to string stra gives: hello c-string

hello c-stringso

input a character string
long long ago, there is girl, she's name is little redhat..
before line: long long ago, there is girl, she's name is little redhat..

quick sort line: ttttssssrrrooonnnmllllliiiihhhggggeeeeeedaaa..,,'

测试string ss,下面给出ss的字符串:

    maybe you are long long girl, i'm who are you.12563.

4
0
55
4294967295
计算string ss中的 o 个数:5
string ss的长度: 56,  string ss中的字母和数字: 39




测试strcspn()函数,待测试的字符串pszTest: long long ago, there is girl, she'name
 is little redhat
长度:55---firt_not_of length `xyza` 10


测试strtok函数(linux下请使用函数·char *strsep (char ** __stringp, const char *
 __delim)·),待测试的字符串:
now#is the time for all#####good men to come to the#aid of their country

now
is the time for all
good men to come to the
aid of their country



测试函数void * memchr (void * ptr, int value, size_t num );

The test str:
now#is the time for all#####good men to come to the#aid of their country
>>Character 'e' is found at 10.
>>e time for all#####good men to come to the#aid of their country



测试strerror(),创造一个error: press any key continue...

Open file crashed, Error code 2: No such file or directory


测试strcasecmp()函数,输入一只动物吧!(dog,cat,etc...)
pig
A pig is any of the animals in the genus Sus, within the Suidae family of even-t
oed ungulates. Pigs include the domestic pig, its ancestor the wild boar, and se
veral other wild relatives. Pigs are omnivores and are highly social and intelli
gent animals.
输入一些什么东西吧

lol.file
>>lol.file
mystring:
got some string here.
after append of mystring:
got some string here. some append string.
请按任意键继续. . .










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值