1
#include <stdio.h> void strinput(char *st, int n); int main() { char input[20]; strinput(input, 20); puts(input); system("pause"); return 0; } void strinput(char *st, int n) { int i; char ch; for (i = 0; i < n - 1; i++) { ch = getchar(); if (ch != EOF) st[i] = ch; else break; } st[i] = '\0'; }
2
#include <stdio.h> void strinput(char *st, int n); int main() { char input[20]; strinput(input, 20); puts(input); system("pause"); return 0; } void strinput(char *st, int n) { int i; char ch; for (i = 0; i < n - 1; i++) { ch = getchar(); if (ch == ' ' || ch == '\t' || ch == '\n') break; else { st[i] = ch; } } st[i] = '\0'; }
3
#include <stdio.h> #include <ctype.h> char *word(char *st); int main() { char Word[80]; while (word(Word) != NULL) puts(Word); system("pause"); return 0; } char *word(char *st) { char ch; while ((ch = getchar()) != EOF && isspace(ch)) continue; if (ch == EOF) return NULL; *st++ = ch; while ((ch = getchar()) != EOF && !isspace(ch)) *st++ = ch; *st = '\0'; if (ch == EOF) return NULL; else { while (ch != '\n') ch = getchar(); } return st; }
4
#include <stdio.h> #include <ctype.h> char *word(char *st, int n); int main() { char Word[10]; while (word(Word, 10) != NULL) puts(Word); system("pause"); return 0; } char *word(char *st, int n) { char ch; int i = 0; while ((ch = getchar()) != EOF && isspace(ch)) continue; if (ch == EOF) return NULL; st[i] = ch; i++; while ((ch = getchar()) != EOF && !isspace(ch) && i < n-1) { st[i] = ch; i++; } st[i] = '\0'; if (ch == EOF) return NULL; else { while (ch != '\n') ch = getchar(); } return st; }
5
#include <stdio.h> char *find(char *str1, char str2); char *s_gets(char *st, int n); int main() { char value[80]; char ch; puts("请输入内容: "); s_gets(value, 80); while (value[0] != '\0') { puts("请输入你要查找的字符: "); ch = getchar(); while (getchar() != '\n') continue; printf("%c : %p\n", ch, find(value, ch)); puts("请输入内容: "); s_gets(value, 80); } puts("完成!"); system("pause"); return 0; } char *find(char *str1, char str2) { char *ptr; ptr = NULL; while (*str1 != '\0') { if (*str1 == str2) { ptr = str1; break; } str1++; } return ptr; } char *s_gets(char *st, int n) { char *ret_val; ret_val = fgets(st, n, stdin); if (ret_val) { while (*st != '\n'&&*st != '\0') st++; if (*st == '\n') *st = '\0'; else while (getchar() != '\n') continue; } return ret_val; }
6
#include <stdio.h> int is_within(char *str1, char str2); char *s_gets(char *st, int n); int main() { char ch[80]; char n; puts("请输入内容: "); s_gets(ch, 80); while (ch[0] != '\0') { puts("请输入你要查找的字符: "); n = getchar(); while (getchar() != '\n') continue; if (is_within(ch, n)) puts("真"); else puts("假"); puts("请输入内容: "); s_gets(ch, 80); } puts("完成退出!"); system("pause"); return 0; } int is_within(char *str1, char str2) { int value = 0; while (*str1 != '\0') { if (*str1 == str2) value = 1; str1++; } return value; } char *s_gets(char *st, int n) { char *ret_val; ret_val = fgets(st, n, stdin); if (ret_val) { while (*st != '\n'&&*st != '\0') st++; if (*st == '\n') *st = '\0'; else while (getchar() != '\n') continue; } return ret_val; }
7
#include <stdio.h> #include <string.h> void mystrncpy(char *str1, char *str2, int n); char *s_gets(char *st, int n); int main() { char word[20]; char word2[30]; puts("请输入你要拷贝的内容: "); s_gets(word2, 30); while (word2[0] != '\0') { mystrncpy(word, word2, 20); printf("输入: "); puts(word2); printf("拷贝: "); puts(word); puts("请输入你要拷贝的内容: "); s_gets(word2, 30); } puts("完成"); system("pause"); return 0; } void mystrncpy(char *str1, char *str2, int n) { int i = 0; while (*str2 != '\0'&&i < n) { *str1 = *str2; str1++; str2++; i++; } if (*str2 == '\0'&&i < n) *str1 = '\0'; } char *s_gets(char *st, int n) { char *ret_val; ret_val = fgets(st, n, stdin); if (ret_val) { while (*st != '\n'&&*st != '\0') st++; if (*st == '\n') *st = '\0'; else while (getchar() != '\n') continue; } return ret_val; }
8
#include <stdio.h> #include <string.h> char *string_in(char *str1, char *str2); char *s_gets(char *st, int n); int main() { char word[40]; char word2[20]; puts("请输入内容: "); s_gets(word, 40); while (word[0] != '\0') { puts("请输入你要查找的字符串: "); s_gets(word2, 20); printf("%s : %p\n", word2, string_in(word, word2)); puts("请输入内容: "); s_gets(word, 40); } puts("完成."); system("pause"); return 0; } char *string_in(char *str1,char *str2) { int i, m; m = strlen(str2); char *ptr; ptr = NULL; while (*str1 != '\0') { if (m == 1) { if (*str1 == *str2) { ptr = str1; return ptr; } } else { for (i = 1; str2[i] != '\0'; i++) { if (*str1 == str2[0]) ptr = str1; if (*str1 == str2[i - 1]) { str1++; } else { break; } } if (i >= m) return ptr; } str1++; } ptr = NULL; return ptr; } char *s_gets(char *st, int n) { char *ret_val; ret_val = fgets(st, n, stdin); if (ret_val) { while (*st != '\n'&&*st != '\0') st++; if (*st == '\n') *st = '\0'; else while (getchar() != '\n') continue; } return ret_val; }
9
#include <stdio.h> void change(char *str); char *s_gets(char *str, int n); int main() { char wold[255]; puts("请输入内容: "); s_gets(wold, 255); while (wold[0] != '\0') { change(wold); puts("请输入内容: "); s_gets(wold, 255); } puts("完成"); system("pause"); return 0; } void change(char *str) { char *ptr; ptr = str; while (*ptr) ptr++; ptr--; do { putchar(*ptr--); } while (ptr - str); putchar(*ptr); putchar('\n'); } char *s_gets(char *str, int n) { char *ch; ch = fgets(str, n, stdin); if (ch) { while (*str != '\n'&&*str != '\0') str++; if (*str == '\n') *str = '\0'; else while (getchar() != '\n') continue; } return ch; }
10
#include <stdio.h> void str_in(char *string); char *s_gets(char *str, int n); int main() { char string[80]; puts("请输入内容:"); s_gets(string, 80); while (string[0] != '\0') { str_in(string); puts(string); puts("请输入内容:"); s_gets(string, 80); } puts("完成"); system("pause"); return 0; } void str_in(char *string) { char *ptr; ptr = string; while (*string != '\0') { while (*string == ' ') { string++; } *ptr = *string; ptr++; string++; if (*string == '\0') *ptr = '\0'; } } char *s_gets(char *str, int n) { char *ch; ch = fgets(str, n, stdin); if (ch) { while (*str != '\n'&&*str != '\0') str++; if (*str == '\n') *str = '\0'; else while (getchar() != '\n') continue; } return ch; }
11
#include <stdio.h> #include <string.h> #include <stdbool.h> int menu(void); char *s_gets(char *str, int n); void stamp_1(char str[][200], int n); void stamp_2(char *str[], int n); void stamp_3(char *str[], int n); void stamp_4(char *str1[], int n); int main() { int i, num; char string[10][200]; char *ptr1[10]; char *ptr2[10]; char *ptr3[10]; char *ptr4[10]; char *ch; i = 0; puts("请输入10组内容[或EOF退出]: "); ch = s_gets(string[i], 200); ptr4[i] = ptr3[i] = ptr2[i] = ptr1[i] = string[i]; for (i = 1; i < 10 && ch != NULL; i++) { ch = s_gets(string[i], 200); ptr4[i] = ptr3[i] = ptr2[i] = ptr1[i] = string[i]; } num = menu(); while (num != 5) { switch (num) { case 1: stamp_1(string, i - 1); break; case 2: stamp_2(ptr1, i - 1); break; case 3: stamp_3(ptr2, i - 1); break; case 4: stamp_4(ptr3, i - 1); break; default: break; } putchar('\n'); num = menu(); } puts("完成退出"); system("pause"); return 0; } int menu(void) { int n; char ch; printf("1. 打印源字符串列表 \t\t2. 以 ASCII 中的顺序打印字符串\n"); printf("3. 按长度递增顺序打印字符串 \t4. 按字符串中第1个单词的长度打印字符串\n"); printf("5. 退出\n"); puts("请选择功能: "); while (scanf_s("%d", &n) == 0 || (n <= 0 || n > 5)) { while ((ch = getchar()) != '\n') continue; puts("输入错误, 请输入1-5: "); } return n; } char *s_gets(char *str, int n) { char *ch; ch = fgets(str, n, stdin); if (ch) { while (*str != '\n'&&*str != '\0'&&*str != EOF) str++; if (*str == EOF) { ch = NULL; *str = '\0'; return ch; } if (*str == '\n') *str = '\0'; else while (getchar() != '\n') continue; } return ch; } void stamp_1(char str[][200], int n) { int i; for (i = 0; i < n; i++) { puts(*str++); } } void stamp_2(char *str[], int n) { int i, j; char *ptr; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (strcmp(str[j], str[j + 1]) > 0) { ptr = str[j]; str[j] = str[j + 1]; str[j + 1] = ptr; } } } for (i = 0; i < n; i++) { puts(str[i]); } } void stamp_3(char *str[], int n) { int i, j; char *ptr; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (strlen(str[j]) > strlen(str[j + 1])) { ptr = str[j]; str[j] = str[j + 1]; str[j + 1] = ptr; } } } for (i = 0; i < n; i++) { puts(str[i]); } } void stamp_4(char *str1[], int n) { int i, j, m; int ch[10]; int num; char *ptr; char *str2[10]; bool word; for (i = 0; i < n; i++) { str2[i] = str1[i]; word = false; for (j = 0, m = 0; str1[i][j] != '\0'; j++) { if (str1[i][j] != ' ') { word = true; m++; } if (str1[i][j] == ' '&&word == true) break; } ch[i] = m; } for (i = 0; i < n-1; i++) { for (j = 0; j < n-1; j++) { if (ch[j] > ch[j + 1]) { num = ch[j]; ch[j] = ch[j + 1]; ch[j + 1] = num; ptr = str2[j]; str2[j] = str2[j + 1]; str2[j + 1] = ptr; } } } for (i = 0; i < n; i++) { puts(str2[i]); } }
15
#include <stdio.h> #include <ctype.h> int myatoi(char *str); void s_gets(char *str, int n); int main() { char ch[50]; int num; puts("请输入内容: "); s_gets(ch, 50); num = myatoi(ch); printf("你输入的内容为: %d\n", num); system("pause"); return 0; } int myatoi(char *str) { char num[20]; int n = 0; int i = 0; int j; for (j = 0; str[j] != '\0'; j++) { if (isdigit(str[j])) { num[i] = str[j]; i++; } else return n; } sscanf_s(num, "%d", &n); //把字符串num转换成整行并存储到n中 return n; } void s_gets(char *str, int n) { char *ret_val; ret_val = fgets(str, n, stdin); if (ret_val) { while (*str != '\n'&&*str != '\0') str++; if (*str == '\n') *str = '\0'; else while (getchar() != '\n') continue; } }
16
#include <stdio.h> #include <ctype.h> int main(int argc,char *argv[]) { char ch; if (argc > 2) { printf("用法: %s [-p | -u | -l]\n", argv[0]); } else { if (argc == 1) { while ((ch = getchar()) != EOF) { putchar(ch); } } else if (argv[1][0] == '-') { if (argv[1][2] != '\0') { goto bk; } switch (argv[1][1]) { case 'p': while ((ch = getchar()) != EOF) { putchar(ch); } break; case 'u': while ((ch = getchar()) != EOF) { ch = toupper(ch); putchar(ch); } break; case 'l': while ((ch = getchar()) != EOF) { ch = tolower(ch); putchar(ch); } break; default: printf("用法: %s [-p | -u | -l]\n", argv[0]); printf("错误: %s 是无效字符.\n", argv[1]); break; } } else { bk:printf("用法: %s [-p | -u | -l]\n", argv[0]); printf("错误: %s 是无效字符.\n", argv[1]); } } return 0; }