字串Copy to 陣列, 計算字元長度
1. memset 陣列寫入 0 或想寫入的字元
2. strcpy 將字串 copy to 陣列
3. strlen 計算字串長度
Example GCC.
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[100];
int i;
//memset(dest, '\0', sizeof(dest));
memset(dest, '0', sizeof(dest));
strcpy(src, "This is gitbook.net");
strcpy(dest, src);
printf("Final copied string : %s\n", src);
printf("Final copied string2 : %s\n", dest);
i = sizeof(dest);
printf("i : %d\n", i);
size_t length = strlen(dest);
printf("字串長度:%lu\n", length);
return(0);
}
參考資料
https://openhome.cc/Gossip/AlgorithmGossip/StackByLink.htm
2. strcpy 將字串 copy to 陣列
3. strlen 計算字串長度
Example GCC.
#include <stdio.h>
#include <string.h>
int main()
{
char src[40];
char dest[100];
int i;
//memset(dest, '\0', sizeof(dest));
memset(dest, '0', sizeof(dest));
strcpy(src, "This is gitbook.net");
strcpy(dest, src);
printf("Final copied string : %s\n", src);
printf("Final copied string2 : %s\n", dest);
i = sizeof(dest);
printf("i : %d\n", i);
size_t length = strlen(dest);
printf("字串長度:%lu\n", length);
return(0);
}
Example TI-CCS
memset(pValue, 0, sizeof(pValue));
strcpy(pValue, "This is test2");
size_t length = strlen(pValue);
*pLen = length;
參考資料
https://openhome.cc/Gossip/AlgorithmGossip/StackByLink.htm
留言
張貼留言