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';
          }
          運行結果:

          主站蜘蛛池模板: 文化| 仙居县| 蕲春县| 永泰县| 西畴县| 郓城县| 武川县| 巴南区| 松滋市| 武城县| 紫阳县| 固安县| 怀仁县| 连州市| 思南县| 广河县| 凤翔县| 凯里市| 祁东县| 汉川市| 浦江县| 汝州市| 正镶白旗| 元朗区| 柳江县| 衡山县| 景谷| 阳东县| 正镶白旗| 安康市| 九江县| 高雄市| 莒南县| 加查县| 大田县| 洪江市| 潜山县| 龙胜| 寻甸| 锦州市| 万年县|