C语言常用字符串操作应用总结

(一)字符串操作

strcpy(p, p1) 复制字符串

原型:strcpy(char destination[], const char source[]);
功能:将字符串source拷贝到字符串destination中
例程:

#include <stdio.h>
#include <string.h>
int main()
{
	char str1[10] = { "ABC "};
	char str2[10] = { "ab"};
	printf("%s",strcpy(str1,str2));
}

运行结果:ab

strncpy(p, p1, n) 复制指定长度字符串

原型:strncpy(char destination[], const char source[], int numchars);
功能:将字符串source中前numchars个字符拷贝到字符串destination中

#include <stdio.h>
#include <string.h>
int main()
{
	char str1[10] = { "ABCDE"};
	char str2[10] = { "abcd"};
	printf("%s",strncpy(str1,str2,3));
}

运行结果:abcDE

strcat(p, p1) 附加字符串

原型:strcat(char target[], const char source[]);
功能:将字符串source接到字符串target的后面
例程:

#include <stdio.h>
#include <string.h>
int main()
{
	char str1[10] = { "ABC"};
	char str2[10] = { "ab"};
	printf("%s", strcat (str1,str2));
}

运行结果:ABCab

strncat(p, p1, n) 附加指定长度字符串

原型:strncat(char target[], const char source[], int numchars);
功能:将字符串source的前numchars个字符接到字符串target的后面
例程:

#include <stdio.h>
#include <string.h>
int main()
{
	char str1[10] = { "ABC"};
	char str2[10] = { "ab"};
	printf("%s", strncat (str1,str2,1));
}

运行结果:ABCa

strlen( p) 取字符串长度

原型:strlen( const char string[] );
功能:统计字符串string中字符的个数
例程:

#include <stdio.h>
#include <string.h>
int main()
{
	char str1[10] = { "ABC"};
	printf("%d", strlen (str1));
}

运行结果:3

strcmp(p, p1) 比较字符串

原型:int strcmp(const char *string1, const char *string2)
功能:比较字符串string1和string2大小,返回值< 0, 表示string1小于string2;

strcasecmp忽略大小写比较字符串

strncmp(p, p1, n) 比较指定长度字符串

strchr(p, c) 在字符串中查找指定字符

strrchr(p, c) 在字符串中反向查找

strstr(p, p1) 查找字符串

原型:char *strstr(const char *string, const char *strSearch);
功能:在字符串string中查找strSearch子串. 返回子串strSearch在string中首次出现位置的指针. 如果没有找到子串strSearch, 则返回NULL. 如果子串strSearch为空串, 函数返回string值;

#include <stdio.h>
#include <string.h>
int main()
{
	char *pStr;
	if((pStr=strstr("ABC=abc","=a"))!=NULL)
	{
		printf("%s",pStr);
	}
}

运行结果:=abc

strpbrk(s1, s2) 字符串(s1)中找出最先含有搜索字符串(s2)中任一字符

原型:char *strpbrk(const char *str1, const char *str2)
功能:检索字符串 str1 中第一个匹配字符串 str2 中字符的字符,不包含空结束字符。也就是说,依次检验字符串 str1 中的字符,当被检验字符在字符串 str2 中也包含时,则停止检验,并返回该字符位置。

strspn(s1, s2) 字符串中第一个不在指定字符串中出现的字符

(二)字符串转数值

atoi( p) 字符串转换到 int 整型

原型: int atoi(const char *str)
功能:把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。

#include <stdio.h>
#include <stdlib.h>
int main()
{
	int val;
	val = atoi("12,3");
	printf("%d",val);
}

输出结果:12

atof( p) 字符串转换到 double 符点数

atol( p) 字符串转换到 long 整型

strtol(p, ppend, base) 八/十/十六进制的字符串转换成long整型

#include <stdio.h>
#include <stdlib.h>
int main()
{
   printf("16进制的数值=%lx\r\n",strtol("0x123", NULL, 16));
   printf("10进制的数值=%ld\r\n",strtol("123A", NULL, 10));
   return(0);
}

输出结果:16进制的数值=123
10进制的数值=123

(三)字符检查

isalpha() 检查是否为字母字符

原型:int isalpha(int c);
功能:检查所传的字符是否是字母。如果是一个字母,则该函数返回非零值,否则返回 0。

#include <stdio.h>
#include <ctype.h>
int main()
{
   if(isalpha('d'))
   {
	printf("d是字母\r\n");
   }
   if(!isalpha('='))
   {
	printf("=不是是字母\r\n");
   }
   return(0);
}

运行结果:d是字母
=不是是字母

isupper() 检查是否为大写字母字符

islower() 检查是否为小写字母字符

isdigit() 检查是否为数字

isxdigit() 检查是否为十六进制数字表示的有效字符

isspace() 检查是否为空格类型字符

iscntrl() 检查是否为控制字符

ispunct() 检查是否为标点符号

isalnum() 检查是否为字母和数字

(四)格式化字符串

sscanf(s,format…)读取格式化的字符串中的数据

原型:int sscanf(const char *str, const char *format, …)
功能:从字符串读取格式化输入。

#include <stdio.h>
int main()
{
   int day, year;
   char weekday[20], month[20], dtm[100];

   sscanf( "Saturday March 25 1989", "%s %s %d  %d", weekday, month, &day, &year );
   printf("%s %d, %d = %s\n", month, day, year, weekday );
   return(0);
}

输出结果:March 25, 1989 = Saturday

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值