有问题欢迎留言讨论~如果要书pdf可以在评论里留下邮箱
IDE: Xcode
11-1
#include <stdio.h>
void save_in(char *array, int n)
{
int i;
for(i = 0; i < n; i++)
array[i] = getchar();
//用getchar输入时键盘上的tab占1字节,因为是char类型,而sizeof('\t')占4字符,是因为字符常量储存为int类型
}
int main()
{
char a[100];
int n;
int i;
printf("How many characters will you enter?\n");
scanf("%d", &n);
getchar();
save_in(a, n);
printf("reprintf for test:\n");
for(i = 0; i < n; i++)
printf("%c", a[i]);
printf("\n");
return 0;
}
Output:
11-2
#include <stdio.h>
int save_in(char *array, int n)
{
int i, input = 0;
for(i = 0; i < n; i++){ //n个字符停止
array[i] = getchar();
if(array[i] == '\n' || array[i] == '\t' || array[i] == ' ') //遇到空白、制表符、换行符停止
break;
else
input++;
}
while(getchar() != '\n');
return input; //return实际存储的字符数
}
int main()
{
char a[100];
int n, actual_get;
int i;
printf("How many characters will you enter?\n");
scanf("%d", &n);
getchar();
actual_get = save_in(a, n);
printf("reprintf for test:\n");
for(i = 0; i < actual_get; i++)
printf("%c", a[i]);
printf("\n");
return 0;
}
Output:
11-3
#include <stdio.h>
#define WORD_MAX_LEN 30
int get_word(char *array, int max)
{
char a;
int i = 0;
while(1){ //第一个while,跳过第1个非空白字符前所有的空白
a = getchar();
if(a != '\n' && a != '\t' && a != ' ')
break;
}
while(i < max){ //第二个while,存储第1个单词
if(a != '\n' && a != '\t' && a != ' '){
array[i] = a;
i++;
a = getchar();
}
else
break;
}
while(getchar() != '\n'); //第三个while,丢弃输入行的其余字符
return i; //return word的长度
}
int main()
{
char word[WORD_MAX_LEN];
int word_len, i;
printf("Please enter:");
word_len = get_word(word, WORD_MAX_LEN);
printf("Word:");
for(i = 0; i < word_len; i++)
printf("%c", word[i]);
printf("\n");
return 0;
}
Output:
11-4
#include <stdio.h>
#define WORD_MAX_LEN 30
int get_word(char *array, int max)
{
char a;
int i = 0;
while(1){ //第一个while,跳过第1个非空白字符前所有的空白
a = getchar();
if(a != '\n' && a != '\t' && a != ' ')
break;
}
while(i < max){ //第二个while,存储第1个单词,并限制了可读取的最大字符数
if(a != '\n' && a != '\t' && a != ' '){
array[i] = a;
i++;
a = getchar();
}
else
break;
}
while(getchar() != '\n'); //第三个while,丢弃输入行的其余字符
return i; //return word的长度
}
int main()
{
char word[WORD_MAX_LEN];
int word_len, i, n;
printf("Please enter the max word len:");
scanf("%d", &n);
getchar();
printf("Please enter:");
word_len = get_word(word, n);
printf("Word:");
for(i = 0; i < word_len; i++)
printf("%c", word[i]);
printf("\n");
return 0;
}
Output:
11-5
#include <stdio.h>
#define STRING_MAX_LEN 100
char* search_in_string(char* s, char c)
{
while(*s != '\0' && *s != c)
s++;
return *s == c ? s : NULL; //#define NULL ((void *)0)
}
void change_to_string(char* s, int max_len)
{
int i;
for(i = 0; i < max_len; i++){
if(s[i] == '\n'){ //如果字符串中有换行符,替换成空字符
s[i] = '\0';
break;
}else if(i == (max_len - 1)){ //如果字符串没有换行符,在字符串末端添加空字符
s[i] = '\0';
}
}
}
int main()
{
char quit;
char string[STRING_MAX_LEN];
char character;
char* result;
while(1){
printf("Start(enter S), else quit:");
scanf("%c", &quit);
getchar();
if(quit != 'S'){
break;
}else{
printf("Please enter string:");
fgets(string, STRING_MAX_LEN,stdin);
change_to_string(string, STRING_MAX_LEN);
printf("Please enter the character you want to be looked up for:");
scanf("%c", &character);
getchar();
result = search_in_string(string, character);
if(result == NULL)
printf("Can not found.\n");
else
printf("found:%s \n", result);
}
}
return 0;
}
Output:
11-6
#include <stdio.h>
#define STRING_MAX_LEN 100
int is_within(char* s, char c)
{
while(*s != '\0' && *s != c)
s++;
return *s == c ? 1 : 0; //找到返回非零值,否则返回0
}
void change_to_string(char* s, int max_len)
{
int i;
for(i = 0; i < max_len; i++){
if(s[i] == '\n'){ //如果字符串中有换行符,替换成空字符
s[i] = '\0';
break;
}else if(i == (max_len - 1)){ //如果字符串没有换行符,在字符串末端添加空字符
s[i] = '\0';
}
}
}
int main()
{
char quit;
char string[STRING_MAX_LEN];
char character;
int result;
while(1){
printf("Start(enter S), else quit:");
scanf("%c", &quit);
getchar();
if(quit != 'S'){
break;
}else{
printf("Please enter string:");
fgets(string, STRING_MAX_LEN,stdin);
change_to_string(string, STRING_MAX_LEN);
printf("Please enter the character you want to be looked up for:");
scanf("%c", &character);
getchar();
result = is_within(string, character);
if(result)
printf("found.\n");
else
printf("Can not found.\n");
}
}
return 0;
}
Output:
11-7
#include <stdio.h>
#include <string.h>
#define STRING_MAX_LEN 100
char* mystrncpy(char* trg, char* src, int copy_len, int src_len)
{
strncpy(trg, src, copy_len);
if(copy_len < (src_len + 1)){
trg[copy_len - 1] = '\0';
}
return trg;
}
void change_to_string(char* s, int max_len)
{
int i;
for(i = 0; i < max_len; i++){
if(s[i] == '\n'){ //如果字符串中有换行符,替换成空字符
s[i] = '\0';
break;
}else if(i == (max_len - 1)){ //如果字符串没有换行符,在字符串末端添加空字符
s[i] = '\0';
}
}
}
int main()
{
char src[STRING_MAX_LEN];
char trg[STRING_MAX_LEN];
int copy_cnt;
int string_len;
char quit;
while(1){
printf("Start(enter S), else quit:");
scanf("%c", &quit);
getchar();
if(quit != 'S'){
break;
}else{
printf("Please enter string:");
fgets(src, STRING_MAX_LEN,stdin);
change_to_string(src, STRING_MAX_LEN);
string_len = (int)strlen(src);
printf("Please enter how many characters you want to be copied:");
scanf("%d", ©_cnt);
getchar();
mystrncpy(trg, src, copy_cnt, string_len);
printf("copy string:%s\n", trg);
}
}
return 0;
}
Output:
11-8
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define STRING_MAX_LEN 100
//在s1中查找s2
char* string_in(char* s1, char* s2, int s1_len, int s2_len)
{
bool is_found = false,flag = false;
int pos_in_s1 = 0,pos_in_s2 = 0;
int i = 0;
if(s1_len < s2_len)
is_found = false;
while(s1[i] != '\0'){
//如果在s1找到s2中的字符,先记录下pos_in_s1,将flag置为true,再继续比较s2剩下的字符
//如果剩下的字符也一样,不修改pos_in_s1,就一直向后比较
//如果比较剩下的字符遇到不一样,就把pos都还原成0,flag也置为false,重新比较
//确认在s1中找到完整的s2,才会把is_found置为true,然后退出
//如果在s1中找到s2的字符,但还未找全,s1就找完了,退出,且is_found不会置为true
if(s1[i] == s2[pos_in_s2]){
if(!flag){
pos_in_s1 = i;
flag = true;
}
pos_in_s2++;
}else{
pos_in_s1 = 0;
pos_in_s2 = 0;
flag = false;
}
i++;
if(pos_in_s2 == s2_len){//如果在s1中找到完整的s2,就退出
is_found = true;
break;
}
}
//is_found = flase,表示没找到,返回空指针, is_found = true,返回在s1的地址
return is_found ? &s1[pos_in_s1] : NULL;
}
void change_to_string(char* s, int max_len)
{
int i;
for(i = 0; i < max_len; i++){
if(s[i] == '\n'){ //如果字符串中有换行符,替换成空字符
s[i] = '\0';
break;
}else if(i == (max_len - 1)){ //如果字符串没有换行符,在字符串末端添加空字符
s[i] = '\0';
}
}
}
int main()
{
char string1[STRING_MAX_LEN];
char string2[STRING_MAX_LEN];
char *found;
int str1_len, str2_len;
char quit;
while(1){
printf("Start(enter S), else quit:");
scanf("%c", &quit);
getchar();
if(quit != 'S'){
break;
}else{
printf("Please enter a string(lengh < 100):");
fgets(string1, STRING_MAX_LEN,stdin);
change_to_string(string1, STRING_MAX_LEN);
str1_len = (int)strlen(string1);
printf("Please enter a string you want to look for in the previous string:");
fgets(string2, STRING_MAX_LEN,stdin);
change_to_string(string2, STRING_MAX_LEN);
str2_len = (int)strlen(string2);
found = string_in(string1, string2, str1_len, str2_len);
if(found != NULL)
printf("found string:%s\n", found);
else
printf("Not found\n");
}
}
return 0;
}
Output:
11-9
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define STRING_MAX_LEN 100
void reverse_string(char* s, int len)
{
int i;
char temp;
for (i = 0; i < (len /2); i++){
temp = s[i]; //头尾交换
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
}
void change_to_string(char* s, int max_len)
{
int i;
for(i = 0; i < max_len; i++){
if(s[i] == '\n'){ //如果字符串中有换行符,替换成空字符
s[i] = '\0';
break;
}else if(i == (max_len - 1)){ //如果字符串没有换行符,在字符串末端添加空字符
s[i] = '\0';
}
}
}
int main()
{
char string[STRING_MAX_LEN];
int str_len;
char quit;
while(1){
printf("Start(enter S), else quit:");
scanf("%c", &quit);
getchar();
if(quit != 'S'){
break;
}else{
printf("Please enter a string(lengh < 100):");
fgets(string, STRING_MAX_LEN,stdin);
change_to_string(string, STRING_MAX_LEN);
str_len = (int)strlen(string);
reverse_string(string, str_len);
printf("reverse string:%s\n", string);
}
}
return 0;
}
Output:
11-10
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define STRING_MAX_LEN 100
void delete_str_blank(char* s, int len)//只应用输入的字符串
{
int i = 0, j = 0;
char* s_no_blank;
s_no_blank = s; //新建一个指针也指向这个字符串
while(i <= len){ //用‘<=’是因为空字符也要挪
//i是指针s的位置,j是指针s_no_blank的位置
//如果当前字符不是空格,i & j都加加,且将s[i]的值赋给s_no_blank[j]
//如果当前字符是空格,i++, j不加,相当于把去掉空字符的其他字符都向前挪
if(s[i] != ' '){
s_no_blank[j] = s[i];
j++;
}
i++;
}
}
void change_to_string(char* s, int max_len)
{
int i;
for(i = 0; i < max_len; i++){
if(s[i] == '\n'){ //如果字符串中有换行符,替换成空字符
s[i] = '\0';
break;
}else if(i == (max_len - 1)){ //如果字符串没有换行符,在字符串末端添加空字符
s[i] = '\0';
}
}
}
int main()
{
char string[STRING_MAX_LEN];
int str_len;
while(1){
printf("Please enter a string(lengh < 100):");
fgets(string, STRING_MAX_LEN,stdin);
change_to_string(string, STRING_MAX_LEN);
str_len = (int)strlen(string);
delete_str_blank(string, str_len);
if(strlen(string)){
printf("delete blank string:%s\n", string);
}else{
printf("quit\n"); //要是空行,去掉空格,strlen = 0,退出
break;
}
}
return 0;
}
Output:
11-11
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define STRING_MAX_LEN 100
#define STRING_MAX_CNT 10
void change_to_string(char* s, int max_len);
void print_src_str(char s[][STRING_MAX_LEN], int str_cnt);
void print_in_ascii_order(char s[][STRING_MAX_LEN], int str_cnt);
bool cmp_in_ascii_order(char* s1, char* s2);
void print_in_ascend_len_order(char s[][STRING_MAX_LEN],int* s_len, int str_cnt);
void bubble_sort(int* src_array, int* order, int len);
void print_in_first_word_len_order(char s[][STRING_MAX_LEN], int str_cnt);
int main()
{
char string[STRING_MAX_CNT][STRING_MAX_LEN];
int str_len[STRING_MAX_CNT];
int i;
char choice;
char quit;
while(1){
for(i = 0; i < STRING_MAX_CNT; i++){ //没考虑EOF,到时候在文件输入输出那章再考虑
printf("Enter No.%d string(lengh < 100):", i);
fgets(string[i], STRING_MAX_LEN,stdin);
change_to_string(string[i], STRING_MAX_LEN);
str_len[i] = (int)strlen(string[i]);
}
while(1){
printf("==============================================================\n");
printf("a. Print the list of source strings\n");
printf("b. Print strings in ASCII order\n");//这一项我理解的是按照string首字母的ascii顺序排序
printf("c. Print strings in ascending order of length\n");
printf("d. Print strings by the length of the first word in the string\n");
printf("e. quit\n");
printf("Enter the operation of your choice:");
choice = getchar();
getchar(); //读取缓冲区enter键
printf("----------------------------------------------------\n");
switch(choice){
case 'a':
print_src_str(string, STRING_MAX_CNT);
break;
case 'b':
print_in_ascii_order(string, STRING_MAX_CNT);
break;
case 'c':
print_in_ascend_len_order(string, str_len, STRING_MAX_CNT);
break;
case 'd':
print_in_first_word_len_order(string, STRING_MAX_CNT);
break;
default:
break;
}
if(choice == 'e')
break;
}
printf("Enter new list of string(enter S), else quit:");
quit = getchar();
getchar();
if(quit != 'S')
break;
}
}
void change_to_string(char* s, int max_len)
{
int i;
for(i = 0; i < max_len; i++){
if(s[i] == '\n'){ //如果字符串中有换行符,替换成空字符
s[i] = '\0';
break;
}else if(i == (max_len - 1)){ //如果字符串没有换行符,在字符串末端添加空字符
s[i] = '\0';
}
}
}
void print_src_str(char s[][STRING_MAX_LEN], int str_cnt)
{
int i;
for(i = 0; i < str_cnt; i++){
printf("%s\n", s[i]);
}
}
void print_in_ascii_order(char s[][STRING_MAX_LEN], int str_cnt)
{
int order[STRING_MAX_CNT]; //因为不能改变原字符串列表,新建一个用来记录顺序的数组
int i, j, temp;
for(i = 0; i < STRING_MAX_CNT; i++)
order[i] = i;
for(i = 0; i < str_cnt - 1; i++){ //冒泡排序
for(j = 0; j < (str_cnt - 1 - i); j++){
if(cmp_in_ascii_order(s[order[j]], s[order[j+1]])){ //返回正,表明第一个字符串按ascii码应该排在第二个字符串后面
temp = order[j];
order[j] = order[j+1];
order[j+1] = temp;
}
}
}
for(i = 0; i < str_cnt; i++){
printf("%s\n", s[order[i]]);
}
}
bool cmp_in_ascii_order(char* s1, char* s2)
{
while(!((*s1 == '\0') && (*s2 == '\0'))){
if(*s1 > *s2){
return true;//返回正,表明第一个字符串按ascii码应该排在第二个字符串后面
}else if(*s1 < *s2){
return false;//返回负,表明第一个字符串按ascii码应该排在第二个字符串前面
}
s1++;
s2++;
}
//如果一直相等
return false;
}
void print_in_ascend_len_order(char s[][STRING_MAX_LEN],int* s_len, int str_cnt)
{
int order[STRING_MAX_CNT]; //新建一个数组用来记录经过长度排序后顺序
int i;
bubble_sort(s_len, order, str_cnt);
for(i = 0; i < str_cnt; i++){
printf("%s\n", s[order[i]]);
}
}
void bubble_sort(int* src_array, int* order, int len) //冒泡排序
{
int i, j, temp;
for(i = 0; i < len; i++)
order[i] = i;
for(i = 0; i < len - 1; i++){ //冒泡排序
for(j = 0; j < (len - 1 - i); j++){
if(src_array[order[j]] > src_array[order[j+1]]){
temp = order[j];
order[j] = order[j+1];
order[j+1] = temp;
}
}
}
}
void print_in_first_word_len_order(char s[][STRING_MAX_LEN], int str_cnt)
{
int order[STRING_MAX_CNT]; //新建一个数组用来记录经过第一个单词的长度排序后顺序
int first_word_len[STRING_MAX_CNT];//新建一个数组用来记录第一个单词的长度
int i, j;
for(i = 0; i < str_cnt; i++){
j = 0;
while((s[i][j] != '\0') && (s[i][j] != ' ')){
j++;
}
first_word_len[i] = j;
}
bubble_sort(first_word_len, order, str_cnt);
for(i = 0; i < str_cnt; i++){
printf("%s\n", s[order[i]]);
}
}
Output:
11-12
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
void text_analysis(int* array)
{
char c;
int word_cnt = 0, upper_cnt = 0, lower_cnt = 0, punch_cnt = 0, digit_cnt = 0;
bool word_flag = false;
//用输入模拟文件结尾,ctrl+d两次
while(1){
c = getchar();
//单词数统计方法,就是后面遇到不是字符就加1
if(isalpha(c))
word_flag = true;
else if(word_flag == true){
word_flag = false;
word_cnt++;
}
if(isupper(c))
upper_cnt++;
else if(islower(c))
lower_cnt++;
else if(ispunct(c))
punch_cnt++;
else if(isdigit(c))
digit_cnt++;
else if(c == EOF)
break;
}
array[0] = word_cnt;
array[1] = upper_cnt;
array[2] = lower_cnt;
array[3] = punch_cnt;
array[4] = digit_cnt;
}
int main()
{
int array[5];
//array[0] = 单词数
//array[1] = 大写字母数
//array[2] = 小写字母数
//array[3] = 标点符号数
//array[4] = 数字字符数
text_analysis(array);
printf("\n--------------------------------------\n");
printf("This text include %d words, %d upper letters, %d lower letters,"
"%d punctuation marks, %d digits.\n", array[0],array[1],array[2],array[3],array[4]);
return 0;
}
Output:
11-13
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#define STRING_MAX_LEN 30
#define STRING_MAX_CNT 20
void reverse_word(char string[])
{
int word_list[STRING_MAX_CNT][2];//声明一个二维数组,存下每个单词的位置和长度
int word_cnt = 0, word_len = 0;
int i = 0 , j = 0;
bool word_flag = false;
while(string[i] != '\0'){
if(isalpha(string[i])){
if(word_flag == false) { //word_flag从false变成true,表示一个单词开始
word_flag = true;
word_list[word_cnt][0] = i; //存下单词的起始位置
}
word_len++; //如果连续是字母,单词长度就一直加
}else{
if(word_flag == true){ //word_flag从true变成flase,表示一个单词结束
word_flag = false;
word_list[word_cnt][1] = word_len; //存下单词的长度
word_cnt++;
word_len = 0; //每次新的单词就把length清零
}
}
i++;
}
while(1){
word_cnt--;
for(j = 0; j < word_list[word_cnt][1]; j++){
putchar(string[word_list[word_cnt][0] + j]);
}
putchar(' ');
if(word_cnt == 0)
break;
}
printf("\n");
}
int main()
{
char text[STRING_MAX_CNT * STRING_MAX_LEN + 20]; //提前分配空间:限定20个单词,每个单词不超过30个字母,加上空格
printf("Please enter a command line parameter of up to 20 words and up to 30 letters per word:\n");
fgets(text, STRING_MAX_CNT * STRING_MAX_LEN + 20, stdin);
reverse_word(text);
return 0;
}
Output:
11-14
//没有命令行环境,用程序模拟实现自定义命令行 -power
#include <stdio.h>
#include <ctype.h>
#include <string.h>
double power(double base_num, int exp){
double power = 1;
int i = 0;
for(i = 0; i < exp; i++){
power *= base_num;
}
return power;
}
int main()
{
printf("NOTE: -power , parameter1:double base number, parameter2: int exponent\n");
char cmd_check[7] = {"-power"};
char quit[3] = {"-q"};
char cmd[10];
char c;
double base, result;
int exp;
while(1){
printf("enter cmd(if quit,enter -q):\n");
scanf("%s", cmd);
if(!strcmp(cmd, quit)) //先检查命令行是否要退出,如果等于-q,返回0,就break
break;
if(strcmp(cmd, cmd_check)){ //检查命令行是否等于-power,若不等于,则返回非零值
while(1){
c = getchar(); //将缓冲区清除,并要求重新输入命令行
if(c == '\n')
break;
}
printf("wrong cmd.\n");
continue;
}
scanf("%lf", &base); //接收命令行第一个参数
scanf("%d", &exp); //接收命令行第二个参数
getchar();
result = power(base, exp);
printf("%lf^%d = %lf\n", base, exp, result);
}
return 0;
}
Output:
11-15
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
int atoi_work(char* string){
bool is_num = true;
int i = 0;
while(string[i] != '\0'){
if(!isdigit(string[i])){
is_num = false;
break;
}
i++;
}
if(is_num)
return 1;
else
return 0;
}
int main()
{
char text[100];
char quit[3] = {"-q"};
while (1) {
printf("please enter a sentence(if quit enter -q):\n");
scanf("%s", text);
getchar();
if(!strcmp(text, quit)) //先检查命令行是否要退出,如果等于-q,返回0,就break
break;
if(atoi_work(text)){
printf("This string is pure number.\n");
}else{
printf("This string is not pure number.\n");
}
}
return 0;
}
Output:
11-16
//没有命令行环境,用程序模拟实现自定义命令行
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define STRING_MAX_LEN 100
void change_to_string(char* s, int max_len)
{
int i;
for(i = 0; i < max_len; i++){
if(s[i] == '\n'){ //如果字符串中有换行符,替换成空字符
s[i] = '\0';
break;
}else if(i == (max_len - 1)){ //如果字符串没有换行符,在字符串末端添加空字符
s[i] = '\0';
}
}
}
void print_p(char* string){
printf("%s\n", string);
}
void toupper_u(char* string){
int i = 0;
while(string[i] != '\0'){
if(islower(string[i]))
putchar(toupper(string[i]));
else
putchar(string[i]);
i++;
}
printf("\n");
}
void tulower_l(char* string){
int i = 0;
while(string[i] != '\0'){
if(isupper(string[i]))
putchar(tolower(string[i]));
else
putchar(string[i]);
i++;
}
printf("\n");
}
int main()
{
char cmd_p_check[3] = {"-p"};
char cmd_u_check[3] = {"-u"};
char cmd_l_check[3] = {"-l"};
char quit[3] = {"-q"};
char cmd[10];
char text[STRING_MAX_LEN];
printf("enter text:\n");
fgets(text, STRING_MAX_LEN,stdin);
change_to_string(text, STRING_MAX_LEN);
while(1){
printf("enter cmd(if quit,enter -q):\n");
scanf("%s", cmd);
getchar();
if(!strcmp(cmd, quit)) //先检查命令行是否要退出,如果等于-q,返回0,就break
break;
else if(!strcmp(cmd, cmd_p_check)){
print_p(text);
}else if(!strcmp(cmd, cmd_u_check)){
toupper_u(text);
}else if(!strcmp(cmd, cmd_l_check)){
tulower_l(text);
}else{
printf("wrong cmd.\n");
}
}
return 0;
}
Output: