1、
#include <stdio.h> #define SIZE 100 void get_n(char * ar, int n); int main(void) { int n; printf("n = "); scanf("%d", &n); while(getchar() != ' ') continue; char st1[SIZE]; get_n(st1, n); puts(st1); int i; for(i = 0; i < n; i++) { printf("st1[%d]: %c ", i, st1[i]); } return 0; } void get_n(char * ar, int n) { int i; puts("please input the elements of the strings."); for(i = 0; i < n; i++) { scanf("%c", &ar[i]); } }
#include <stdio.h> #define SIZE 40 char * read_char(char *st, int n); int main(void) { char test[SIZE]; puts("Start to test function, enter a string."); read_char(test, 5); puts("The string you input is: "); puts(test); return 0; } char * read_char(char *st, int n) { int i = 0; do { st[i] = getchar(); } while(st[i] != EOF && ++i < n); return 0; }
2、
#include <stdio.h> #define SIZE 40 char * read_char(char *st, int n); int main(void) { char test[SIZE]; puts("Start to test function, enter a string."); read_char(test, 5); puts("The string you input is:"); puts(test); return 0; } char * read_char(char *st, int n) { int i = 0; do { st[i] = getchar(); if(st[i] == ' ' || st[i] == ' ' || st[i] == ' ') break; } while(st[i] != EOF && ++i < n); return st; }
3、
#include <stdio.h> #define SIZE 100 char * get_word(char *st, int n); int main(void) { char word[SIZE]; char * temp; temp = get_word(word, SIZE); puts(temp); return 0; } char * get_word(char *st, int n) { char * temp; int i = 0; temp = fgets(st, n, stdin); if(temp) { if(st[0] == ' ') { while(st[i] == ' ') i++; } } temp += i; while(st[i] != ' ' && st[i] != ' ' && st[i] != ' ') i++; st[i] = '