point 小练习题 一

/*
猜数:首先生成一个介于1-100之间的整数,从键盘
不断输入数值直到该数值与生成的那个数相符为止,
并打印出猜数的次数(在主函数中定义intsecret用
于存放待猜数据,设计函数
void create_secret(int*secretp)用于生成待猜数据)
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void create_secret(int *secretp)
{

	srand(time(NULL));
	*secretp = rand()%100 + 1;
}


void guess(int n)
{
	int a;
	int count = 0;

	while(1){
		scanf("%d", &a);
		if(a == n){
			count++;
			break;
		}
		if(a > n){
			count++;
			printf("num too high\n");
		}
		if(a < n){
			count++;
			printf("num too low\n");
		}
	}

	printf("Get it by %d\n", count);
}

int main(void)
{
	int n;
	create_secret(&n);
	guess(n);

	return 0;
}

/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./1
84
num too high
55
num too high
32
num too high
22
num too low
28
num too high
26
Get it by 6
*/








/*
使用指针完成数组元素的存数及数据的反向输出
*/

#include <stdio.h>

int main(void)
{
	int st[10];
	int i, *p;
	int j;
	p = st;

	for(i = 0; i < 10; i++){
		scanf("%d", (p + i));
	}

	
	for(j = i - 1; j >= 0; j--){
		printf("%d ", *(p + j));
	}

	printf("\n");



	return 0;
}

/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./3
2 3 5 6 8 9 7 55 6 2
2 6 55 7 9 8 6 5 3 2 
*/











/*
函数实现:生成26个排列好的英文字母
*/

#include <stdio.h>

int main(void)
{
	char st[26];
	char *p;
	int i;

	p = st;

	for(i = 0; i < 26; i++){
		*(p + i) = 'a' + i;
	
	}

	printf("%s\n", p);


	return 0;
}

/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./4
abcdefghijklmnopqrstuvwxyz
*/







/*
函数实现:删除字符数组中的空格
*/
#include <stdio.h>
#define N 1024

char *del_space(char *src)
{
	int i, j;
	for(i = j = 0; src[i] != '\0'; i++){
		if(src[i] != ' '){
			src[j++] = src[i];
		//	j++;
		}

	}

	src[j] = '\0';

	return src;

}





int main(void)
{
	char str[N];

	fgets(str, N, stdin);
	del_space(str);
	puts(str);

	return 0;
}

/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./5.1
hello world ni hao
helloworldnihao

akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ 
*/













/*
函数实现:删除字符数组中的空格
扩展:删除数组中的指定字符
*/
#include <stdio.h>
#define N 1024

char *del_space(char *src, char c)
{
	int i, j;
	for(i = j = 0; src[i] != '\0'; i++){
		if(src[i] != c){
			src[j++] = src[i];
		//	j++;
		}

	}

	src[j] = '\0';

	return src;

}





int main(void)
{
	char str[N];
	char c;

	printf("Enter a string\n");
	fgets(str, N, stdin);

	printf("Enter del's character\n");
	scanf("%c", &c);

	del_space(str, c);

	puts(str);


	return 0;
}


/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./5.2
Enter a string
hello world ni hao
Enter del's character
h
ello world ni ao

akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ 
*/<pre name="code" class="cpp">



/*
函数实现:删除字符数组中的空格(扩展:删除数组中的指定字符)
*/


#include <stdio.h>
#include <string.h>
#define N 1024

char *del(char *src);
//char dest[1024];

int main(void)
{
//	char str[] = "hello world haha"; 
	char str[N];

	fgets(str, N, stdin);
	printf("%s\n", del(str));
	return 0;
}

char *del(char *p)
{
	int  i, j;
	static char dest[1024];
	for(i = j = 0; *(p + i) != '\0'; i++){
		if( *(p + i) != ' ' ){
			dest[j] = *(p + i);
			j++;
		}
	}

	dest[j] = '\0';

	return dest;
}



/*
akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$ ./5
hello world
helloworld

akaedu@akaedu-G41MT-D3:~/lin/719_point/mypoint$*/

 
 
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值