编写一个程序,将一串字符倒序存放后输出。
生活随笔
收集整理的這篇文章主要介紹了
编写一个程序,将一串字符倒序存放后输出。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
源代碼:
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char *p;int i,n; 6 cout<<"請輸入字符串的長度n=";cin>>n; 7 p=new char[n]; 8 for(i=0;i<n;i++) 9 cin>>p[i]; 10 for(i=0;i<n/2;i++) 11 { 12 char t=p[i];p[i]=p[n-i-1];p[n-i-1]=t; 13 } 14 for(i=0;i<n;i++) 15 cout<<p[i]; 16 cout<<endl;delete[] p; 17 return 0; 18 }
運行結果:
代碼2:
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 char t,a[]="VISUALC++PROGRAM"; 6 int i; 7 for(i=0;i<16;i++) 8 cout<<a[i]; 9 cout<<endl; 10 for(i=0;i<8;i++) 11 { 12 t=a[i];a[i]=a[16-i-1];a[16-i-1]=t; 13 } 14 cout<<"倒序后的數組:"<<endl; 15 for(i=0;i<16;i++) 16 cout<<a[i]; 17 cout<<endl; 18 return 0; 19 }運行結果:
轉載于:https://www.cnblogs.com/iamvirus/archive/2012/06/28/2567835.html
總結
以上是生活随笔為你收集整理的编写一个程序,将一串字符倒序存放后输出。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows Phone 7 系统主题
- 下一篇: Cloudstack部署