c语言中怎么从字符串中取字符

c语言中怎么从字符串中取字符

char s[]="ssssabedbewb";
int len;
char *p;
s[2]; //第一种方法
printf("输入输入字符串的长度:");
scanf("%d",&len);
printf("输入字符串:");
p=malloc(len);
scanf("%s",p);
printf("%c",p[3]); //第二种方法

显然第二种方法更灵活,不过就是有点烦
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-02-22
用标准c库中的字符串操作函数就可以了
需要#include
"string.h"
常用的函数有strcpy,strlen,strcmp,strchr,strstr等等
第2个回答  推荐于2017-10-11
要include <string>

定义一个字符串 string s = "asd";
获取字符直接用 s[0]就行了本回答被提问者采纳
第3个回答  2010-04-20
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char s[] = "abcde";
int n = strlen(s);
int i;
for( i=0; i<n; i++ )
{
putchar(s[i]);
}

return 0;
}
第4个回答  2010-04-18
使用数组下标或者指针
比如p[1]或者*(p+1)等
相似回答