Posted on 2007-07-04 18:42
停留的風 閱讀(226)
評論(0) 編輯 收藏 所屬分類:
C語言學習歷程
字符數組的定義:
char word[]="Hello!";
或者:
char word[]={"Hello!"};
或者:
char word[7]={'H','e','l','l','o','!','\0'};
注意:C語言中所有的常數字符串都是以空字符結尾的,這樣很多常用的函數才能知道字符串的結尾在哪里。
#include <stdio.h>
int main(void)
{
void concat(char result[],const char str1[],const char str2[]);
const char s1[]={"Test "};
const char s2[]={"works!"};
char s3[20];
concat(s3,s1,s2);
printf("%s\n",s3);//盡管s3中有20個元素,但使用了%s格式描述符調用printf函數顯示。
//%s是用來顯示空字符結的字符串;
return 0;
}
void concat(char result[],const char str1[],const char str2[])
{
int i,j;
for(i=0;str1[i]!='\0';i++)
{
result[i]=str1[i];
}
for(j=0;str2[j]!='\0';j++)
{
result[i+j]=str2[j];
}
result[i+j]='\0';
}
運行結果:
