【C语言】将字符串顺序翻转,如: I am a student 转成:student a am I
//将字符串顺序翻转 //如: I am a student 转成:student a am I #include <stdio.h> #include <string.h> void fanw( char *left,char *right ) //把每个单词单词翻转 { char* pleft = left; char* pright = right; char temp; while( pleft < pright ) { temp = *pleft; *pleft = *pright; *pright = temp; pleft++; pright--; } } void fans( char *p ) //在字符串中找出单词 { while( *p != '\0') { char *pst = p; while( *p != '\0' && *p != ' ' ) { p++; } fanw( pst,p-1 ); p++; } } int main() { char p[30] = "student a am i"; int len = strlen(p); printf("原字符串为 : %s\n",p); printf("翻转后的字符串为 :"); fanw(p,p+len-1); fans(p); printf("%s\n",p); return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。