1. 程式人生 > >練習:常用字符串

練習:常用字符串

love std 技術 第一個 return str src \n mage

/*#include <string.h>

1、strlen 計算字符串長度;

2、strcop 拷貝;

3、strncop 拷貝指定數量的字符;

4、shrcat 連接字符串;*/

#include <stdio.h>
#include <string.h>
int main(void)
{
char a[64];
int k;
printf("請輸入一個字符串:");
gets(a);
k = strlen(a);
printf("字符串長度是:%d\n",k);
return 0;
}

技術分享圖片

#include <stdio.h>
#include <string.h>
int main(void)
{
char a[64] = "love";
char b[64] = "thinks";
//strcop的第一個參數,是目標字符串;
//strcop的第二個參數,是源字符串;
strcpy(a,b);
printf("輸出結果:%s\n",a);

return 0;
}

技術分享圖片

#include <stdio.h>
#include <string.h>
int main(void)
{
char a[64] = "love";
char b[64] = "123456";
//strcop的第一個參數,是目標字符串;
//strcop的第二個參數,是源字符串;
strncpy(a,b,3); //strncpy函數是拷貝指定數量的字符串;
printf("輸出結果:%s\n",a);

return 0;
}

技術分享圖片

#include <stdio.h>
#include <string.h>
int main(void)
{
char a[64];
char b[64];
printf("輸入一個字符:");
gets(a);
printf("輸入一個字符:");
gets(b);
strcat(a,b); //strcat是連接字符串
printf("輸出結果:%s\n",a);
return 0;
}

技術分享圖片

練習:常用字符串