Viết chương trình đảo ngược các từ trong chuỗi
Ex:
nguyen van a
==> a van nguyen
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char *s = new char[100];
gets(s);
int a[100000],x=0,b[100000],y=0,f=0;
for(int i = 0 ; i < strlen(s) ; i++)
{
if(*(s+i) != ' ')
a[x++] = (int)*(s+i);
if(*(s+i) == ' ')
{
a[x++] = (int)*(s+i);
for(int i = x-1 ; i >= 0 ; i--)
b[y++] = a[i];
x = 0;
}
f++;
}
if(f == strlen(s))
{
b[y++] = (int)' ';
for(int i = x-1 ; i >= 0 ; i--)
b[y++] = a[i];
x = 0;
}
for(int i = y-1 ; i >= 0 ; i--)
cout<<(char)b[i];
return 0;
}