请教C语言字符串倒序输出

来源:百度知道 编辑:UC知道 时间:2024/05/21 08:18:49
//这个程序功能包括:1. 读入字符串 2. 倒序输出 3. 统计单词个数
#include<stdio.h>
#include<string.h>
void main()
{
char string1[200]; //用于存放输入的字符串
char string2[200]; //用于存放倒序后的字符串
int invertion(char *ch1, char *ch2); //声明函数

printf("Please input a sentences:\n");
scanf("%s", string1);
printf("Your inputed sentences is:%s", string1);

invertion(string1, string2);
printf("The invertion sentences is%s:\n", string2);

}

int invertion(char *ch1, char *ch2)/*char1 接收实参传过来的原字符串指针 char2 接收倒序后的新字符串返回主函数*/
{
int count = 0;
char *end = ch2;
char temp;

while(*ch1) // 统计单词个数
{
if(*ch1++ !=' ')
{
count++;
while(*ch1 != '/0' && *ch1 != ' ')
ch1++;
}
printf("count = %d", count);
}

while(*end)
{
end++;
end

#include<stdio.h>
#include<string.h>
void main()
{
char string1[200]; //用于存放输入的字符串
char string2[200]; //用于存放倒序后的字符串
int invertion(char *ch1, char *ch2); //声明函数
printf("Please input a sentences:\n");
gets(string1); //这里不要用scanf,因为scanf遇到空白符就会结束
printf("Your inputed sentences is:%s\n", string1);

invertion(string1, string2);
printf("The invertion sentences is:%s\n", string2);
getchar();
}

int invertion(char *ch1, char *ch2)/*char1 接收实参传过来的原字符串指针 char2 接收倒序后的新字符串返回主函数*/
{
int count = 1,num=0;
char *ch=ch1;
for(;*ch1!='\0';ch1++)// 统计单词个数
{
if(*ch1==' ')
count++; //单词数等于空格数加1,前面把count初始化为1就是这个原因
if(*ch1==' '&&*(ch1+1)==' ') //防止单词之间有2个空格符而多计数了一个单词数
count--;
}
printf("count = %d\n", count);
ch1=ch1-1;//前面的ch1经过循环之后已经指向字符串的结束