字符串练习题一

/*
定义char str[2][20],从键盘输入两个字符
串保存在此数组中,求这两个字符串中的最大字符
*/

#include <stdio.h>
#include <string.h>

int main(void)
{
	char str[2][20];
	int i, j;
	char max_c;

	for(i = 0; i < 2; i++){
		scanf("%s", str[i]); // str[i]  ==   char(*)[20]
	}
	for(i = 0; i < 2; i++){
		printf("%s ", str[i]);
	}
	printf("\n");
	
	max_c = str[0][0];
	for(i = 0; i < 2; i++){
		for(j = 0; str[i][j] != '\0'; j++){
			if(str[i][j] > max_c ) {
					max_c = str[i][j];
			}
		}
	}


	printf("%c\n", max_c);

	return 0;
}

/*
dawxd adaz
dawxd adaz 
z
*/<pre name="code" class="cpp">


/*
自己编写程序实现void itoa(int x, char *str)(将数字x,
转换为字符串并保存到str中,即如:54639 转成‚54639‛)
*/
#include <stdio.h>
#include <string.h>

void itoa(int x, char *str)
{
//	char str[20];
	int i=0, j;
	char str_l[20];
	do{
		str[i] = x%10 + '0';
		i++;
	
	}while( (x = x/10) != 0 );

	str[i] = '\0';
	str_l[i] = str[i];

	for(--i, j = 0; i >= 0 ; i--, j++ ){
				str_l[j] = str[i];
	}

	strcpy(str, str_l); 
	printf("%s\n", str);
}

int main(void)
{
	int n;
	char str[20];
	scanf("%d", &n);
	itoa(n, str);

	return 0;
}

/*
akaedu@akaedu-G41MT-D3:~/lin/721_string$ ./2
785511
785511
*/<pre name="code" class="cpp">

/*
统计子串substr在源串src中出现的次数。
源串及子串数据从命令行参数获取。
*/

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
	char *retp, *begin;
	int count = 0;
	int substrlen = 0;
	retp = strstr(argv[1], argv[2]);
    
	if(argc != 3){
		printf("Usage:./a.out substr src\n");
		return 0;
	}


	if(argv[2][0] == '\0'){
		printf("count = 0\n");
		return 0;
	}


	for(begin = argv[1], substrlen = strlen(argv[2]); (retp = strstr(begin, argv[2]));
														begin = retp + substrlen ){
														
			count++;												
		}


	printf("count:%d\n", count);


	return 0;

}

/*
akaedu@akaedu-G41MT-D3:~/lin/721_string$ ./4 adadadad ''
count = 0
akaedu@akaedu-G41MT-D3:~/lin/721_string$ ./4 adadadad '' a
Usage:./a.out substr src
akaedu@akaedu-G41MT-D3:~/lin/721_string$ ./4 adadadad ad
count:4


*/<pre name="code" class="cpp">


/*
若有字符串http://www.google.cn/search?complete=1&hl=zh-CN&ie=GB2312&q=linux&meta=
用于进行数据搜索,使用库函数分别解析出key与value数据。程序运行后显示:
key = complete value = 1
key = hl value = zh-CN
…
*/

#include <stdio.h>
#include <string.h>

int main(void)
{
	char str[] = "http://www.google.cn/search?complete=1&hl=zh-CN&ie=GB2312&q=linux&meta=";
	char *value, *begin, *key;
	puts(str);

	begin = strchr(str, '?') + 1;

	for(; (key = strtok(begin, "&")); begin = NULL ){
		key = strtok_r(key, "=", &value);
		if( !key ){        //(key == 0)
			break;
		}
		printf("key:%s value:%s\n", key, value);
	}


	return 0;
}

/*
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./2
http://www.google.cn/search?complete=1&hl=zh-CN&ie=GB2312&q=linux&meta=
key:complete value:1
key:hl value:zh-CN
key:ie value:GB2312
key:q value:linux
key:meta value:
*/





 
 
 
 
 
 

你可能感兴趣的:(字符串练习题一)