字符串处理用法-c语言

版权声明:转载请注明出处: https://blog.csdn.net/qq_38701476/article/details/82927507

介绍一下有关字符串的异界常用函数

  1. strcpy(char des[], const char src[]);

    功能:对字符串进行复制,有src复制到des

  2. strncpy(char destination[], const char source[], int numchars);

    功能:同上复制,能够指定负值多少个长度,可以运行以下代码感受一下

#include<stdio.h>
#include<string.h>
int main()
{
	char m[20],n[20],z[20];
	scanf("%s",m);
	printf("%s\n%s\n%s\n",m,n,z);
	strcpy(n,m);
	printf("%s\n%s\n%s\n",m,n,z);
	strncpy(n+1,m,3);
	printf("%s\n%s\n%s",m,n,z);
	return 0;
}
  • 注意:c中\0代表字符串结尾,但是也占一个位置.因此,如果源src没有复制到最后的话并且des最后这个位置有字符的化,是不会覆盖上去的.
#include<stdio.h>
#include<string.h>
int main()
{
	char m[20],n[20],z[20];
	scanf("%s",m);
	n[3]='1';
	strncpy(n,m,3);
	printf("%s\n%s\n",m,n);
	return 0;
}

当输入123后,会发现复制完n会变成1231而不是123

  1. strcat(char target[], const char source[]);

    功能:将字符串source接到字符串target的后面

  2. strncat(char target[], const char source[], int numchars);

    功能:将字符串source的前numchars个字符接到字符串target的后面

  3. int strcmp(const char a[], const char b[]);

    功能:比较两个字符串a和b ,返回负值则a<b,正值a>b,相等则返回0

  4. strlen( const char string[] );

    功能:统计字符串string长度,不包括’\0’;

  5. 常用字符串与数值类型转换

    atoi ( p ) 字符串转换到 int 整型
    atof ( p ) 字符串转换到 double 符点数
    atol ( p ) 字符串转换到 long 整型

  6. 字符检查

    isalpha() 检查是否为字母字符
    isdigit() 检查是否为数字
    isupper() 检查是否为大写字母字符
    islower() 检查是否为小写字母字符
    ispunct() 检查是否为标点符号
    isalnum() 检查是否为字母和数字

猜你喜欢

转载自blog.csdn.net/qq_38701476/article/details/82927507