C语言 字符串知识整理

#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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值