C语言程序设计(第二版),练习2-4&&练习2-5&&练习2-9练习2-10
来源:互联网 发布:网络编辑没学历能做吗 编辑:程序博客网 时间:2024/05/09 07:16
练习2-4
#include <stdio.h>#define MAXLINE 1000 //maximun of amount of array//2-4void Squeeze(char s1[], char s2[]);int main() { char s1[MAXLINE];//array s1 char s2[MAXLINE];//array s2 int c; int length_s1 = 0;//memory the length of array s1 int length_s2 = 0;//memory the length of array s1 for (int i=0; i < MAXLINE - 1 && (c = getchar()) != EOF; ++i) {//input array s1 s1[i] = c; length_s1++; } s1[length_s1] = '\0';//set the final array element as empty printf("the process of input of s1 completed\n"); for (int i=0; i < MAXLINE - 1 && (c = getchar()) != EOF; ++i) {//input array s2 s2[i] = c; length_s2++; } s2[length_s2] = '\0';//set the final array element as empty printf("the process of input of s2 completed\n"); Squeeze(s1, s2);}void Squeeze(char s1[], char s2[]) { int i = 0; int j = 0; while (s1[i] != '\0' && s2[j] != '\0') {//scan array s1 and array s2 if (s1[i] == s2[j]) { for(int k=i; s1[k] != '\0'; ++k) { s1[k] = s1[k+1];//delete the common element from array s1 } ++j;//set the location of array s1 as the same location of orignal array s1 } i++; j++; } printf("the new s1 :%s\n", s1);}
练习2-5
#include <stdio.h>#define MAXLINE 1000 //maximun of amount of array//2-5int Any(char s1[], char s2[]);int main() { char s1[MAXLINE];//array s1 char s2[MAXLINE];//array s2 int c;//input label int length_s1 = 0;//memory the length of array s1 int length_s2 = 0;//memory the length of array s2 int location;//return location for (int i=0; i < MAXLINE - 1 && (c = getchar()) != EOF; ++i) {//input array s1 s1[i] = c; length_s1++; } s1[length_s1] = '\0';//set the final array element as empty printf("the process of input of s1 completed\n"); for (int i=0; i < MAXLINE - 1 && (c = getchar()) != EOF; ++i) {//input array s2 s2[i] = c; length_s2++; } s2[length_s2] = '\0';//set the final array element as empty printf("the process of input of s2 completed\n"); location = Any(s1, s2); printf("the location is %d", location);}int Any(char s1[], char s2[]) { int i = 0; int j = 0; printf("s1 : %s", s1); printf("s2 : %s", s2); while (s2[i] != '\0') {//scan array s1 and array s2 while(s1[j] != '\0') { if(s2[i] != s1[j]) ++j;//search the same element in array s2 else return j+1; } ++i; } return -1;}
练习2-9
#include <stdio.h>int bitcount(unsigned int x) { int bitcount_ = 0; if(x > 0) {//when x > 0, then exist one single digit "one" in x at least bitcount_ = 1; while ((x &= (x - 1)) != 0) ++bitcount_;//plus one when x decrean } return bitcount_;}int main() { int count = 0; unsigned int x_ = 7; count = bitcount(x_); printf("%d\n", count);}
练习2-10
#include <stdio.h>//2-10int lower(int c_) { return c_ = (c_ >= 'A' && c_ <= 'Z') ? (c_ + 'a' - 'A') : c_;}int main() { int c; int uppercase_c = 0; if ((c = getchar()) != EOF) { uppercase_c = lower(c); } putchar(uppercase_c); return 0;}
0 0
- C语言程序设计(第二版),练习2-4&&练习2-5&&练习2-9练习2-10
- 练习(2-3)-C语言程序设计
- [练习]:LCA练习2
- C语言程序设计练习2(窗口服务指示系统3.5 )
- 浙大版《C语言程序设计(第3版)》题目集(函数题)练习5-2
- //2.11练习.练习4(2).p37
- 《C语言程序设计(第二版新版)》课后练习
- 第二章 2.4.4节练习 & 2.5.2节练习
- c练习2
- c语言练习2
- C语言练习(2)
- C语言练习2
- C/C++练习2
- C 基础练习2
- 练习2
- 练习2
- 练习2
- 练习2
- C++ :: 作用域
- 用nohup命令让Linux下程序永远在后台执行
- JEECG企业微云快速二次开发平台-新版本首页展示
- POJ 3259 Wormholes
- Spring事务配置的5中方式
- C语言程序设计(第二版),练习2-4&&练习2-5&&练习2-9练习2-10
- ./configure,make,make install,make clean的作用
- Linux 技巧:让进程在后台可靠运行的几种方法
- 桎梏的开始01
- 在S1字符串中删除S2字符串出现过的任意字符
- c++学习笔记(15.类模板)
- 这样密集的负能量段子,看起来实在是太爽啦!!!
- javascript:void(0)
- poj 1083 DP