#include <stdio.h>
void main()
{
int i;
char s[10];
gets(s);
puts(s);
return 0;
}
此代码用于快速输入输出字符串,可以保留空格,且不用单独使用头函数
#include <stdio.h>
void main()
{
char a[]="good vdvf"
"fjdsalfjda";
printf("%s",a);
//当字符串太长可拆分成片段写在多行
}
#include <stdio.h>
int main()
{
char *p;
scanf("%s",p);
printf("%s",p);
//没有取地址符,因为p本来就是是地址
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[100]={"hello"};
char *p;
p=&a;//或p=a
printf("%c",*(p+1));//输出单个字符,*意为取内容
printf("%s",p+1); //输出字符串,地址后面的;
return 0;
}
//数组名是地址常量不可用str++来使其指向字符串的某个字符
字符串长度在定义时要比实际长度大一,确保程序正确
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *q;
scanf("%s",q);
printf("%s",q);
return 0;
}
不用分配空间
用scanf和printf
fgetsa(name,sizeof(name),stdin)
可限制输入字符串长度,使设计更安全
字符处理函数可用于指针
int a;
char *p = "this is for testing";
a=strlen(p);
printf("%d",a);
return 0;
//此代码可行
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int a;
char n[100]= {"hello"};
a=strlen(n);
printf("%d",a);
//字符处理函数
return 0;
}
------------------------------------------
★★★★★★★★★★★★★★★★★★★
极其重要
//多组字符串的输入,输出;
#include <stdio.h>
#include <stdlib.h>
int main()
{
char a[100][100];
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s",&a[i]);
}
for(i=1;i<=n;i++)
{
printf("%s\n",a[i]);
}
return 0;
}
★★★★★★★★★★★★★★★★★★★
-------------------------------------------
字符串的转换: -‘0’
字符‘0’对应的ASCII码是48,48对应的十六进制数就是0x30,通常我们在编程的时候,用字符转化为数字的时候经常要用到,比如要将‘8’转换为数字8,在语句中这样写就可以了,“ 8+‘0’”。这里的8就是数字。
2021/4/17