欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: strncat函數實現及簡單應用 [打印本頁]

作者: xiaojuan    時間: 2014-9-24 22:33
標題: strncat函數實現及簡單應用

//:Vc++6.0  String  strncat函數
//功能:按照num數量連接num個字符
//參數:dest 目標字符串  str 連接字符串  num 連接個數
//返回值:目標字符串
#include<stdio.h>

char *strncat(char *dest, const char *str, int num);

int main()
{
char str1[20] = "hello ";
char str2[20] = "world haha";
char str3[20] = "heihei";
strncat(str3, strncat(str1, str2, 6), 5);
printf("str1 = %s\n", str3);
return 0;
}

char *strncat(char *dest, const char *str, int num)
{
if (dest == NULL || str == NULL)
{
perror("dest or str");
return NULL;
}
char *temp = dest;
const char *temp1 = str; //前面必須是const修飾的,否則編譯不通過
    int len;
while (*(temp1++) != '\0');
len = temp1 - str;
temp1 = str;

    while (*(temp++) != '\0');
    temp--;

    if (num <= 0) //當num值小于0時,不連接
        *temp = '\0';
    else if (num >= len) //當num值大于字符長度時,全部連接
    {
        while (*(temp1) != '\0')
        {
            *(temp++) = *(temp1++);
        }
        *temp = '\0';
    }
    else
    {
        for (int i = 0; i < num; i++)
        {
            *(temp++) = *(temp1++);
        }
        *temp = '\0';
    }
return dest;
}

//在vc++6.0中的運行結果為:str1 = heiheihello//:~







歡迎光臨 (http://m.raoushi.com/bbs/) Powered by Discuz! X3.1