302班

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

          連接字符串

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

          主站蜘蛛池模板: 青冈县| 蒙阴县| 繁峙县| 普定县| 班戈县| 大港区| 东安县| 武宣县| 襄城县| 天等县| 湄潭县| 宁明县| 灵台县| 陵水| 柯坪县| 甘孜| 呈贡县| 林芝县| 湖南省| 隆昌县| 咸丰县| 遵化市| 米林县| 广州市| 翁牛特旗| 上虞市| 乌鲁木齐市| 汉中市| 永兴县| 晋州市| 扬州市| 盐源县| 天祝| 祁阳县| 盈江县| 华安县| 仁寿县| 巴青县| 弥勒县| 阿坝县| 米泉市|