求高手告知怎么把字符串变成数组。例如输入abcd,然后给一个数组ch[10],让ch[0]=a,ch[1]=b以此类推。本人想了好久都没有思路。
解决方案
25
#include <stdio.h> #include <string.h> int main(void) { char *str = "helloworld"; char arr_str[32]; int len, i; /* one way */ len = strlen(str); strncpy(arr_str, str, len+1); for (i = 0; i < len; i++) putchar(arr_str[i]); putchar(10); /* another way */ memset(arr_str, 0, sizeof(arr_str)); printf("Please input a string: "); scanf("%s", arr_str); for (i = 0; i < len; i++) putchar(arr_str[i]); putchar(10); return 0; }
两种方式都可以
15
MFC 里面: char cBuff[512]; CString str = "ABCDEFG"; memset(cBuff, 0, sizeof(cBuff)); strcpy(cBuff, str ); //这样cBuff[0] = "A" //非MFC环境: char cBuff[512]; string str = "ABCDEFG"; memset(cBuff, 0, sizeof(cBuff)); strcpy(cBuff, str );