有一个字符数组的内容为:"student a am i",请你将数组的内容改为"i am a student". #include<stdio.h> #include<ctype.h> #include<stdlib.h> int my_strlen(const char *s) { char *eos = (char*)s; while (*eos) { eos++; } return (eos - s); } void *my_strcmp(char *arr, char *s) { while (*arr++ = *s++) ; } void getword(char *s,int lim) { char arr[20]; my_strcmp(arr, s); char *sp = arr + lim - 1; int i = 0; char *w = sp; while (*sp != EOF) { w = sp; while (!isspace(*w)&&(--lim)) { w--; sp = w; } w++; if (lim == 0) { while (!isspace(*sp)) { s[i++] = *sp++; } break; } for (i; isalpha(*(w)); w++, i++) { s[i] = *(w); } while (!isalpha(*sp)) { s[i] = *sp--; lim--; } i++; } return; } int main() { char s[] = "student a am i"; int len = my_strlen(s); getword(s,len); printf("%s\n", s); system("pause"); return 0; }
字符数组"student a am i"--》"i am a student"
原文地址:http://lingdandan.blog.51cto.com/10697032/1708218