/*
Reverse String
*/
#include<iostream>
using namespace std;
#define size 100
void main()
{
char *str = new char[size];
char *ptr;
int i = 0,count = 0;
cout<<"input the string: ";
cin.getline(str,size);
cout<<endl<<"Reverse Your String: ";
for(int j=strlen(str)-1;j>=0;j--)
{
cout<<str[j];
}
cout<<endl<<endl;
/*ptr = str;
while( *ptr != NULL )
{
count++;
ptr++;
}
cout<<endl<<"Reverse Your String: ";
for(int j = count-1;j>=0;j--)
cout<<str[j];*/
}
執行結果:
兩種寫法
注解掉的是忘記strlen方法時使用
寫這時雞仔發現一個有趣的小地方
cin.get() & cin.getline()
兩者用法很像結束都是用enter鍵
差別在於
cin.get() 會將結束字元放進陣列中
cin.getline() 則不會
但是雞仔Debug發現
兩者在輸入完字串後都會在後頭加個 ' ' 字元
輸入字串abc
cin.get()
cin.getline
很奇怪 囧? (也沒看到代表enter的 '\n' ...)
全站熱搜
留言列表