標題:
字串處理 (一)
[打印本頁]
作者:
tonyh
時間:
2013-12-14 15:06
標題:
字串處理 (一)
本帖最後由 tonyh 於 2013-12-14 15:38 編輯
我們在宣告字串的時候要注意它的長度,以免位數不夠造成程式錯誤。事實上,每個字串後面都有一個 '\0' 的字元,例如 "tw" 字串,事實上總共用了 3 Bytes,若為中文字,則每個字要佔兩個字元。
例如:
char str[3]="tw";
char str[9]="字串處理";
//字串處理 (4種方式)
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string str1="Hello! My name is Tony!";
cout<<str1<<endl;
char str2[7]={"Hello!"};
cout<<str2<<endl;
char str3[6]={'H','e','l','l','o','!'};
for(int i=0; i<=5; i++)
cout<<str3[i];
cout<<endl;
string str4[5]={"Hello! ","My ","name ","is ","Tony!"};
for(int i=0; i<=4; i++)
cout<<str4[i];
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
林以諾
時間:
2013-12-14 15:10
本帖最後由 林以諾 於 2013-12-15 14:16 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string str1="hello! pig";
cout<<str1<<endl;
char str2[7]={"Hello!"};
cout<<str2<<endl;
char str3[6]={'H','e','l','l','o','!'};
for(int i=0; i<=5; i++)
cout<<str3[i];
cout<<endl;
string str4[4]={"Hello! ","My ","name ","is ","pig",};
for(int i=0; i<=4; i++)
cout<<str4[i];
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
黃崇維
時間:
2013-12-14 15:11
本帖最後由 黃崇維 於 2013-12-14 15:42 編輯
//字串處理 (4種方式)
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string str1="Hello! My name is Tony!";
cout<<str1<<endl;
char str2[7]={"Hello!"};
cout<<str2<<endl;
char str3[6]={'H','e','l','l','o','!'};
for(int i=0; i<=5; i++)
cout<<str3[i];
cout<<endl;
string str4[5]={"Hello! ","My ","name ","is ","Tony!"};
for(int i=0; i<=4; i++)
cout<<str4[i];
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
劉泳鱔
時間:
2013-12-14 15:13
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string str1="Hellow! My name is Elmer";
cout<<str1<<endl;
char str2[7]={"Hellow!"};
cout<<str2<<endl;
char str3[3]={'H','e','l','l','o','w','!'};
for(int i=0; i<=5; i++)
cout<<str[i];
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
張瀚仁
時間:
2013-12-14 15:13
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string str1="Hello my name is tony!";
cout<<str1<<endl;
char str2[7]={"Hello!"};
cout<<str2<<endl;
char str3[6]={'H','e','l','l','o','!'};
for(int i=0; i<=5;i++)
cout<<str3[3];
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
許逸群
時間:
2013-12-14 15:14
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string strl="hi";
cout<<str1<<endl:
shar str2[6]={"Hello!"};
cout<<str2<<endl;
shar str3[6]={'H','e','l','l','o','w','!',};
for(int i=0;pi<=5;i++)
cout<<str3[i;
shout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
郭凡瑛
時間:
2013-12-14 15:18
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string str1="Hello! My name is Tony!";
cout<<str1<<endl;
char str2[7]={"Hello!"};
cout<<str2<<endl;
char str3[6]={'H','e','l','l','o','!'};
for(int i=0; i<=5; i++)
cout<<str3[i];
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
鎧言
時間:
2013-12-14 15:50
//字串處理 (4種方式)
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string str1="Hello! My name is Kyan!";
cout<<str1<<endl;
char str2[7]={"Hello!"};
cout<<str2<<endl;
char str3[6]={'H','e','l','l','o','!'};
for(int i=0; i<=5; i++)
cout<<str3[i];
cout<<endl;
string str4[5]={"Hello! ","My ","name ","is ","Kyan!"};
for(int i=0; i<=4; i++)
cout<<str4[i];
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
郭凡瑛
時間:
2013-12-14 17:04
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string str1="Hello! My name is 人!";
cout<<str1<<endl;
char str2[7]={"Hello!"};
cout<<str2<<endl;
char str3[6]={'H','e','l','l','o','!'};
for(int i=0; i<=5; i++)
cout<<str3[i];
cout<<endl;
string str4[5]={"Hello! ","My ","name ","is ","人!"};
for(int i=0; i<=4; i++)
cout<<str4[i];
cout<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2