C语言 练习5-输入一行字符串(含单词和空格),把字符串翻转


下划线代表空格
思路是:
算出字符串的字符数;
逆序检查,遇见不是空格就存到b数组;
遇到空格或a的下标减到了0(减到0再不输出就没机会了)就把存的逆序输出(逆序存的逆序输出就是正序),然后b数组标号置0;
循环;

#include <stdio.h>
#include <stdlib.h>

void con_print(char a[],int p)//逆序输出
{
	for(p--; p>=0; p--) 
	{
		printf("%c",a[p]);
	}
}
int main()
{
	char a[200],b[200];
	int i,p,count=0;
	while(fflush(stdin),gets(a))
	{
		for(i=0,p=0; i<200; i++)
		{
			if(a[i]!='\0')
			{
				p++;
			}else if(a[i]=='\0'){
				break;
			}
		}

		for(p--; p>=0; p--) //逆序输出
		{
			if(a[p]!=' ')
			{
				b[count++]=a[p];
			}
			if(a[p]==' ' || p==0)
			{
				if(count!=0)
				{
					con_print(b,count);
					count=0;
				}
				printf(" ");
			}
		}
		printf("\n");
	}

	system("pause");
}
发布了35 篇原创文章 · 获赞 2 · 访问量 924

猜你喜欢

转载自blog.csdn.net/qq_45735810/article/details/102962605
今日推荐