C语言字符串温习

#include<stdio.h>
#include<string.h>


int main()
{
  /*字符串strcpy使用,将指针b的内容复制给数组a*/ 
  char a[20];
  char *b="I am a boy!";
  /*strcpy函数的参数为两个指针,
  数组作为函数参数传递时自动退化为相同类型的指针*/ 
  strcpy(a,b); 
  printf("%s\n",a);
  return 0;
  
}

#include<stdio.h>
#include<string.h>


int main()
{
  //strcat字符串拼接 
  char a[20];
  char *I="I";
  char *am=" am";
  char *boy=" boy";
  strcpy(a,I);
  strcat(a,am);
  strcat(a,boy);
  printf("%s\n",a);
  
}


#include<stdio.h>
#include<string.h>


int main()
{
  char a[20];
  char *p;
  char b='m';
  strcpy(a,"I am a boy!");
  p=strchr(a,b);//p为字符m的内存地址 
  if(p)
    printf("%d\n",p-a);
  else
    printf("not find!");
  
}

#include<stdio.h>
#include<string.h>


int main()
{
  char *a="I am a boy!";
  char *b="I am a girl!";
  int c;
  //看Asic码,a>b,返回值 > 0;两串相等,返回0
  
  c=strcmp(a,b);
  if(c>0)
    printf("a<b\n");
  else if(c=0)
    printf("a=b\n");
  else
    printf("a>b\n");
}

#include<stdio.h>
#include<string.h>
#include<alloc.h>


int main()
{ 
  //strdup将字符串a拷贝到新建的位置处 
  char *a="I am a boy!";
  char *b;
  b=strdup(a);
  printf("%s\n",b);
  delete(b);
  return 0; 
}

stricmp以大小写不敏感的方式比较两个字符串;

#include <stdio.h>
#include <string.h>
#include <errno.h>

//返回指向错误信息字符串的指针
int main()
{
  char *a;
  a=strerror(errno);//通过标准错误的标号,获得错误的描述字符串。
  printf("Error:%s\n",a);
  return 0;
}


//当然字符串操作函数很多很多,这里只列出了部分函数的使用和操作!!
#include <stdio.h>
#include <string.h>

int main(void)
{
        char str[]="root:x::0:root:/bin/bash:";
        char *token;
        token=strtok(str,":");
        printf("%s\n",token);
        while((token=strtok(NULL,":"))!=NULL)
                printf("%s\n",token);
        return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值