302班

          java突擊隊
          posts - 151, comments - 74, trackbacks - 0, articles - 14
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          連接字符串

          Posted on 2007-07-04 18:42 停留的風 閱讀(227) 評論(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';
          }
          運行結果:

          主站蜘蛛池模板: 青州市| 武邑县| 分宜县| 玉田县| 海南省| 通城县| 扶余县| 壶关县| 饶河县| 乌兰浩特市| 鱼台县| 永春县| 扶余县| 双柏县| 高要市| 开鲁县| 乌苏市| 南陵县| 肥乡县| 东至县| 蒲江县| 张家港市| 白沙| 霍邱县| 德保县| 鸡泽县| 佳木斯市| 翼城县| 浮山县| 阜南县| 于都县| 柳江县| 神农架林区| 金塔县| 繁峙县| 固原市| 雷山县| 高青县| 平湖市| 石泉县| 福泉市|