编程环境Visual Studio 2017
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 |
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h> // practice 1 void get_input(char *input, int num) { printf("Please enter %d character:", num); for (size_t i = 0; i < num; i++) { input[i] = getchar(); } return; } void p11_1(void) { char input[256]; int number = 0; printf("Enter the number of character:"); scanf_s("%d", &number); getchar(); get_input(input, number); printf("You input:"); for (size_t i = 0; i < number; i++) { printf("%c", input[i]); } printf("\n"); } // practice 2 void get_input_rewrite(char *input, int num) { printf("Please enter:"); char ch = 0; for (size_t i = 0; i < num; i++) { ch = getchar(); if (isblank(ch)) { return; } else { input[i] = ch; } } return; } void p11_2(void) { char input[256]; int number = 0; printf("Enter the number of character:"); scanf_s("%d", &number); getchar(); get_input_rewrite(input, number); printf("You input:"); for (size_t i = 0; i < number; i++) { printf("%c", input[i]); } printf("\n"); } // practice 3 void get_input2(char *input) { char ch = 0; int i = 0; printf("Enter the word:"); do { ch = getchar(); if (!isblank(ch)) { break; } } while (1); input[i++] = ch; do { ch = getchar(); if (isblank(ch)) { break; } input[i++] = ch; } while (1); input[i] = '\0'; return; } void p11_3(void) { char input[256]; get_input2(input); printf("The word you entered is %s\n", input); return; } // practice 4 void get_input3(char *input, int num) { char ch = 0; int i = 0; printf("Enter the word, limited in %d character:", num-1); do { ch = getchar(); if (!isblank(ch)) { break; } } while (1); input[i++] = ch; do { ch = getchar(); if (isblank(ch)) { break; } input[i++] = ch; if (i >= num - 1) { break; } } while (1); input[i] = '\0'; return; } void p11_4(void) { char input[10]; get_input3(input, 10); printf("The word you entered is %s\n", input); return; } // practice 5 int strchr1(char *string, char *chr) { int string_length = strlen(string); int chr_length = strlen(chr); int m = 0; int n = 0; for (size_t i = 0; i < string_length; i++) { n = i; m = 0; while (string[n] == chr[m]) { n++; m++; if (m >= chr_length) { return i; } } } return -1; } void p11_5(void) { char string[256]; char chr[256]; int loc = 0; printf("Enter the string:"); gets_s(string, 256); printf("Enter the string you want to find:"); gets_s(chr, 256); loc = strchr1(string, chr); if (-1 == loc) { printf("%s doesn't appear in %s\n", chr, string); } else { printf("For %s, it is staring from %d character in %s\n", chr, loc, string); } return; } // practice 6 int is_within(char ch, char *string) { int string_length = strlen(string); for (size_t i = 0; i < string_length; i++) { if (ch == string[i]) { return 1; } } return 0; } void p11_6(void) { char ch = 0; char string[256]; _Bool within = 0; while ('q' != ch) { printf("Enter the string:"); gets_s(string, 256); printf("Enter the character:"); ch = getchar(); getchar(); within = is_within(ch, string); if (within == 0) { printf("character %c is not within %s\n", ch, string); } else { printf("character %c is within %s\n", ch, string); } } return; } // practice 7 char *mystrncpy(char *s1, char *s2, int n) { int s1_length = strlen(s1); int s2_length = strlen(s2); int copy_length = (s2_length >= n ? n : s2_length); for (size_t i = 0; i < copy_length; i++) { s1[s1_length + i] = s2[i]; } if (s2_length < n) { s1[s1_length + s2_length] = '\0'; } return s1; } void p11_7(void) { char string1[256]; char string2[256]; int string1_length = 0; int string2_length = 0; while (1) // 一直循环输入吧,也可以添加一个退出条件 { printf("Enter the first string:"); gets_s(string1, 256); printf("Enter the second string:"); gets_s(string2, 256); string1_length = strlen(string1); string2_length = strlen(string2); printf("The first original string: "); puts(string1); mystrncpy(string1, string2, (256 - string1_length)); printf("After mystrncpy, ths first string: "); puts(string1); } return; } // practice 8 // 基本算法和strchr1是一样的,就返回值不同 char *string_in(char *string1, char *string2) { int string_length = strlen(string1); int chr_length = strlen(string2); int m = 0; int n = 0; for (size_t i = 0; i < string_length; i++) { n = i; m = 0; while (string1[n] == string2[m]) { n++; m++; if (m >= chr_length) { return &string1[i]; } } } return NULL; } void p11_8(void) { char string1[256]; char string2[256]; while (1) // 一直循环输入吧,也可以添加一个退出条件 { printf("Enter the first string:"); gets_s(string1, 256); printf("Enter the second string:"); gets_s(string2, 256); char *in = string_in(string1, string2); if (NULL == in) { printf("%s is not at %s\n", string2, string1); } else { printf("%s is at %s, address:%p\n", string2, string1, in); } } return; } // practice 9 void revert_string(char *string) { int string_length = strlen(string); for (size_t i = 0; i < string_length / 2; i++) { char ch = string[i]; string[i] = string[string_length - i - 1]; string[string_length - i - 1] = ch; } return; } void p11_9(void) { char string[256]; while (1) { printf("Enter the string:"); gets_s(string, 256); printf("The string you entered is %s\n", string); revert_string(string); printf("After reverting string, The string is %s\n", string); } return; } // practice 10 void remove_space(char *string) { int string_length = strlen(string); int m = 0; for (size_t i = 0; i < string_length; i++) { if (isspace(string[i])) { continue; } string[m++] = string[i]; } string[m] = '\0'; return; } void p11_10(void) { char string[256]; printf("Enter the string:"); gets_s(string, 256); while ('\0' != string[0]) { printf("The original string you entered is "); puts(string); remove_space(string); printf("After remove space, the string is "); puts(string); printf("Enter the next string:"); gets_s(string, 256); } printf("Done and exit!"); return; } // practice 11 void print_string(char *string[10], int string_amount) { for (size_t i = 0; i < string_amount; i++) { printf("%d: ", i); puts(string[i]); } return; } void print_original_string(char string[][256], int string_amount) { static char *string_ori[10]; // 本质数组,存储十个指向char的指针 static _Bool sort = 0; //记录是否已经处理过,没必要每次都处理吧 if (sort != 1) { for (size_t i = 0; i < string_amount; i++) { string_ori[i] = string[i]; } sort = 1; } printf("Now print the original string:\n"); print_string(string_ori, string_amount); } void print_string_by_ascii(char string[][256], int string_amount) { static char *string_ascii[10]; // 本质数组,存储十个指向char的指针 static _Bool sort = 0; //记录是否已经处理过,没必要每次都处理吧 if (sort != 1) { for (size_t i = 0; i < string_amount; i++) { string_ascii[i] = string[i]; } for (size_t i = 0; i < string_amount; i++) { for (size_t j = i + 1; j < string_amount; j++) { int result = strcmp(string_ascii[i], string_ascii[j]); if (result > 0) { char *tmp = string_ascii[j]; string_ascii[j] = string_ascii[i]; string_ascii[i] = tmp; } } } sort = 1; } printf("Now print the string sorted by ascii:\n"); print_string(string_ascii, string_amount); } void print_string_by_length(char string[][256], int string_amount) { static char *string_length[10]; static _Bool sort = 0; if (sort != 1) { for (size_t i = 0; i < string_amount; i++) { string_length[i] = string[i]; } for (size_t i = 0; i < string_amount; i++) { for (size_t j = i + 1; j < string_amount; j++) { if (strlen(string_length[i]) < strlen(string_length[j])) { char *tmp = string_length[i]; string_length[i] = string_length[j]; string_length[j] = tmp; } } } sort = 1; } printf("Now print the string sorted by string length:\n"); print_string(string_length, string_amount); } int check_first_word_length(char *string) { int result = 0; int string_lenght = strlen(string); for (size_t i = 0; i < string_lenght; i++) { if (isspace(string[i])) { return i; } } return string_lenght; } void print_string_by_first_word_length(char string[][256], int string_amount) { static char *string_first_word_length[10]; static _Bool sort = 0; if (sort != 1) { for (size_t i = 0; i < string_amount; i++) { string_first_word_length[i] = string[i]; } for (size_t i = 0; i < string_amount; i++) { for (size_t j = i + 1; j < string_amount; j++) { if (check_first_word_length(string_first_word_length[i]) < check_first_word_length(string_first_word_length[j])) { char *tmp = string_first_word_length[i]; string_first_word_length[i] = string_first_word_length[j]; string_first_word_length[j] = tmp; } } } sort = 1; } printf("Now print the string sorted by first word length:\n"); print_string(string_first_word_length, string_amount); } void p11_11(void) { char string[10][256]; char choice = 0; int string_amount = 0; printf("You can enter up to ten strings:\n"); for (size_t i = 0; i < 10; i++) { gets_s(string[i], 256); if (string[i][0] == '\0') { break; } string_amount++; } do { printf("Menu:\n"); printf("%-50s%-50s\n%-50s%-50s\n%-50s\n", "a) print original string", "b) print string sorted by ascii", "c) print string sorted by string length", "d) print string sorted by first word length", "q) quit"); printf("Now enter your choice: "); choice = getchar(); getchar(); switch (choice) { case 'a': print_original_string(string, string_amount); break; case 'b': print_string_by_ascii(string, string_amount); break; case 'c': print_string_by_length(string, string_amount); break; case 'd': print_string_by_first_word_length(string, string_amount); break; case 'q': printf("Done and quit!\n"); return; default: printf("Unkonw command, enter again!\n"); break; } } while (1); } // practice 12 void p11_12(void) { char string[256]; int string_length = 0; int n_word = 0; int n_upper = 0; int n_lower = 0; int n_punch = 0; int n_num = 0; printf("Enter the string:"); gets_s(string, 256); string_length = strlen(string); for (size_t i = 0; i <= string_length; i++) { if (isspace(string[i]) || '\0' == string[i]) { n_word++; } else if (isupper(string[i])) { n_upper++; } else if (islower(string[i])) { n_lower++; } else if (ispunct(string[i])) { n_punch++; } else if (isdigit(string[i])) { n_num++; } } printf("String %s contains %d word, %d upper character, %d lower character, %d punchation, %d digital!\n", string, n_word, n_upper, n_lower, n_punch, n_num); return; } // practice 13 void p11_13(int argc, char **argv) { for (size_t i = argc - 1; i > 0; i--) { printf("%s ", argv[i]); } return; } // practice 14 void p11_14(int argc, char **argv) { double base = 0.0; int index = 0; double result = 0.0; base = atof(argv[1]); index = atoi(argv[2]); result = pow(base, index); printf("base:%lf, index:%d, result:%lf", base, index, result); } // practice 15 int atoi_rewrite(char *string) { int string_length = strlen(string); int result = 0; for (size_t i = 0; i < string_length; i++) { if (!isdigit(string[i])) { return 0; } result = result * 10 + (string[i] - '0'); } return result; } void p11_15(void) { char string[256]; int result = 0; printf("Enter a integer number:"); gets_s(string, 256); result = atoi_rewrite(string); printf("string %s to integer %d\n", string, result); } // practice 16 void p11_16(int argc, char **argv) { char string[256]; int string_length = 0; printf("Enter the string:"); gets_s(string, 256); string_length = strlen(string); if (strcmp("-p", argv[1]) == 0 || argc == 1) { printf("Original string: "); puts(string); } else if (strcmp("-u", argv[1]) == 0) { printf("Upper string:"); for (size_t i = 0; i < string_length; i++) { putc(toupper(string[i]), stdout); } printf("\n"); } else if (strcmp("-l", argv[1]) == 0) { printf("Lower string: "); for (size_t i = 0; i < string_length; i++) { putc(tolower(string[i]), stdout); } printf("\n"); } return; } int main(int argc, char **argv) { // p11_16(); p11_16(argc, argv); while (getchar()); return; } |
此文为博主原创文章,转载请注明出处
第五题不是应该返回指针吗?
第7题应该是复制到s1字符串而不是s1的末尾吧? s1[s1_length + i] = s2[i] 这里
书不在身边,不知道题目具体要求是什么?看我自己写的程序貌似是将两个字符串拼接起来,如果题目要求是将两个字符串拼接起来的话,那上面的程序是没问题的,就是函数名起得不好。如果不是将两个字符串拼接的话,那上面的程序就写错了。
要求是把第二个字符串拷贝到第一个是类似strncpy不是strncat拼接。
谢谢,已经改回来了
第二道题要求是如果遇到空白、制表符、换行符时也能停止。被调函数读输入的时候好像是完成了这个要求,但是主调函数打印还是num个单位,调试时空白字符后面几位的数据打印成了莫名其妙的东西。所以要怎么改,我的想法是从被调函数传递空白字符的位置给主调函数循环打印,但是好像改错了...,还是不行。
楼主 我这是根据11题,写出的“按照长度递增顺序打印字符串”,但是运行之后不按照顺序打印,感觉写的没问题,不知道哪里出了问题,楼主能给看下么
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#define LIM 10
#define SIZE 256
void func3(char*st[], int num);//给字符串排序的函数
char*s_gets(char*st, int n);//读入字符串的函数
int main(void)
{
int i = 0;
char input[LIM][SIZE];
char*ptr3[LIM];
while (i < LIM && s_gets(input[i], SIZE))
{
ptr3[i] = input[i];//这样操作,不会改变原有数组
i++;
}
func3(ptr3, i);//函数调用
for (int n = 0; n < i; n++)//打印排序好的字符串
puts(ptr3[n]);
return 0;
}
char*s_gets(char*st, int n)
{
char*ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n'&&st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
void func3(char*st[], int num)
{
char*ct;
int x, y;
for (x = 0; x < num - 1; x++)
{
for (y = x+1; y 0)
{
ct = st[x];
st[x] = st[y];
st[y] = ct;
}
}
}
}
上面的程序复制的有问题 是下面这个
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#define LIM 10
#define SIZE 256
void func3(char*st[], int num);//给字符串排序的函数
char*s_gets(char*st, int n);//读入字符串的函数
int main(void)
{
int i = 0;
char input[LIM][SIZE];
char*ptr3[LIM];
while (i < LIM && s_gets(input[i], SIZE))
{
ptr3[i] = input[i];//这样操作,不会改变原有数组
i++;
}
func3(ptr3, i);//函数调用
for (int n = 0; n < i; n++)//打印排序好的字符串
puts(ptr3[n]);
return 0;
}
char*s_gets(char*st, int n)
{
char*ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (st[i] != '\n'&&st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
void func3(char*st[], int num)
{
char*ct;
int x, y;
for (x = 0; x < num - 1; x++)
{
for (y = x+1; y 0)
{
ct = st[x];
st[x] = st[y];
st[y] = ct;
}
}
}
}
第十六题,第一个if 为啥加上这个条件argc == 1,它是什么作用;为啥三个if里面都用的是argv[1],这题不是有三个命令行参数(-p,-u,-l),我感觉是argv【1】,argv【2】,argv【3】
最后一条 我执行了 为什么会无缘无故退出啊??还有第14题编译之后有一个警告!
我的第5题(感觉比较简洁):
char * strchr2(char * st1,char * st2)
{
int num = strlen(st2);
int i;
while(*st1++)
{
for(i = 0;i < num;i++)
{
if(st1[i] != st2[i])
break;
if(i == num-1)
return st1;
}
}
return NULL;
}