strrchr,求字符串处理函数(全)
strrchr,求字符串处理函数(全)详细介绍
本文目录一览: strchr函数有什么用法?最后输出的结果是什么?
1、功能:查找一个字符c在另一个字符串str中末次出现的位置(也就是从str的右侧开始查找字符c首次出现的位置),并返回从字符串中的这个位置起,一直到字符串结束的所有字符。如果未能找到指定字符,那么函数将返回NULL。
2、输出的结果:cd
函数原型:char *strchr(const char *str, int c)
参数:str-- 要被检索的 C 字符串。c-- 在 str 中要搜索的字符。
strrchr(a,'c')//在被检索的 C 字符串“a”即“abcd” 中搜索字符“c”
扩展资料
strchr函数的实现:
char* strchr(char *s, char c)
{
while(*s != '\0' && *s != c)
{
++s;
}
return *s==c ? s : NULL;
}
参考资料:百度百科 - strchr函数
php怎样去除文件后缀名
php去除文件后缀名的方法:首先利用strrchr()函数查找文件后缀在文件名中出现的位置;然后利用str_replace()函数替换文件后缀名即可。str_replace() 函数替换字符串中的一些字符(区分大小写)。(推荐教程:php图文教程)语法:str_replace(find,replace,string,count)参数:find 必需。规定要查找的值。 replace 必需。规定替换 find 中的值的值。 string 必需。规定被搜索的字符串。 count 可选。一个变量,对替换数进行计数。 strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。语法:strrchr(string,char)(学习视频推荐:php视频教程)参数:string 必需。规定被搜索的字符串。 char 必需。规定要查找的字符。如果该参数是数字,则搜索匹配数字 ASCII 值的字符。 实现代码:
输出结果:help
c语言字符串处理函数的英文原名?
字符串函数都以str开头,是string的缩写
lwr:lowercase 小写
upr:uppercase 大写
cat:catenate 连接
函数名: strrchr
功 能: 在串中查找指定字符的最后一个出现
用 法: char *strrchr(char *str, char c);
举例:
[cpp] view plain copy
char fullname="./lib/lib1.so";
char *ptr;
ptr = strrchr(fullname,'/');
printf("filename is %s",++ptr);
//运行结果:filename is lib1.so
函数名: strchr
功 能: 在串中查找指定字符的第一个出现
用 法: char *strchr(char *str, char c);
举例:
[cpp] view plain copy
char fullname="./lib/lib1.so";
char *ptr;
ptr = strrchr(fullname,'.');
printf("after strchr() is %s",++ptr);
//运行结果:after strchr() is /lib/lib1.so
函数名: strtok
功 能: 在串中查找指定字符的第一个出现
用 法: char *strtok(char *s, char *delim);
说明:
1.strtok函数的实质上的处理是,strtok在s中查找包含在delim中的字符并用NULL(’/0′)来替换,直到找遍整个字符串。这句话有两层含义:(1)每次调用strtok函数只能获得一个分割单位。(2)要获得所有的分割单元必须反复调用strtok函数。
2.strtok函数以后的调用时的需用NULL来替换s.
3.形参s(要分割的字符串)对应的变量应用char s[]=”….”形式,而不能用char *s=”….”形式。
举例:
[cpp] view plain copy
void main()
{
char buf[]=”Golden Global View”;
char* token = strtok( buf, ” “);
while( token != NULL )
{
printf( ”%s “, token );
token = strtok( NULL, ” “);
}
return 0;
}
/*其结果为:
Golden
Global
View
*/
求字符串处理函数(全)
1、字符串长度函数strlen(参数)
解释:求出字符串中有效字符的个数,参数可以是指针、字符串、数组
例如:char aa[]="abc";
char *p=aa;
strlen(aa)=strlen(p)=strlen("abc")
2、字符串比较函数strcmp()和strncmp()
解释:将两个字符串逐个进行比较,当遇到两个字符不同时便停止比较,用前面字符串中的字符与后面字符串中对应的字符进行相减,得到一个大于或小于0的字符,若返回值
为0,则字符串相等。strncmp()只比交前n个字符
格式:strcmp(char *p1,char *p2) strncmp(char *p1,char*p2,n)
p1 p2可以是指针 也可以是字符数组
3、检索字符串函数index()或rindex
解释:该函数的功能是检索在指定的字符串中第一次出现指定字符的位置,该函数返回一个指针,该指针给出指定字符在字符串中的位置,若该字符串中没有该字符则返回NULL 格式:char*index(char *p,charc) char *rindex(char *p,char c) index 从作向右 rindex 从右向左
4、字符串连接函数strcat()和 strncat()
解释:连接两个字符串,将第二个字符串连接到第一个字符串的后面,组成一个新的字符串,并返回一个指针,该指针指向新字符串的首元素
格式:char *strcat(char s1[],char s2[]), char *strncat(chars1[],char s2[],n)
该函数的参数可以是指针 也可以是数组
5、字符串复制函数strcpy()和strncpy()
解释:将后面的字符串复制到前面的字符串中,后面字符串保持不变,前面的字符串被覆盖掉,并返回一个指针
格式:char * strcpy(char s1[],char s2[]) char*strncpy(char s1[],char s2[],n);
参数可以是字符数组或者指针
AddSlashes: 字符串加入斜线。
bin2hex: 二进位转成十六进位。
Chop: 去除连续空白。
Chr: 返回序数值的字符。
chunk_split: 将字符串分成小段。
convert_cyr_string: 转换古斯拉夫字符串成其它字符串。
crypt: 将字符串用 DES 编码加密。
echo: 输出字符串。
explode: 切开字符串。
flush: 清出输出缓冲区。
get_meta_tags: 抽出文件所有 meta 标记的资料。
htmlspecialchars: 将特殊字符转成 HTML 格式。
htmlentities: 将所有的字符都转成 HTML 字符串。
implode: 将数组变成字符串。
join: 将数组变成字符串。
ltrim: 去除连续空白。
md5: 计算字符串的 MD5 哈稀。
nl2br: 将换行字符转成
。
Ord: 返回字符的序数值。
parse_str: 解析 query 字符串成变量。
print: 输出字符串。
printf: 输出格式化字符串。
quoted_printable_decode: 将 qp 编码字符串转成 8 位字符串。
QuoteMeta: 加入引用符号。
rawurldecode: 从 URL 专用格式字符串还原成普通字符串。
rawurlencode: 将字符串编码成 URL 专用格式。
setlocale: 配置地域化信息。
similar_text: 计算字符串相似度。
soundex: 计算字符串的读音值
sprintf: 将字符串格式化。
strchr: 寻找第一个出现的字符。
strcmp: 字符串比较。
strcspn: 不同字符串的长度。
strip_tags: 去掉 HTML 及 PHP 的标记。
StripSlashes: 去掉反斜线字符。
strlen: 取得字符串长度。
strrpos: 寻找字符串中某字符最后出现处。
strpos: 寻找字符串中某字符最先出现处。
strrchr: 取得某字符最后出现处起的字符串。
strrev: 颠倒字符串。
strspn: 找出某字符串落在另一字符串遮罩的数目。
strstr: 返回字符串中某字符串开始处至结束的字符串。
strtok: 切开字符串。
strtolower: 字符串全转为小写。
strtoupper: 字符串全转为大写。
str_replace: 字符串取代。
strtr: 转换某些字符。
substr: 取部份字符串。
trim: 截去字符串首尾的空格。
ucfirst: 将字符串第一个字符改大写。
ucwords: 将字符串每个字第一个字母改大写。
parse_url: 解析 URL 字符串。
urldecode: 还原 URL 编码字符串。
urlencode: 将字符串以 URL 编码。
base64_encode: 将字符串以 BASE64 编码。
base64_decode: 将 BASE64 编码字符串解码。
函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
程序例:
#include
#include
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
printf("%sn", string);
return 0;
}
函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例:
#include
#include
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%sn", destination);
return 0;
}
函数名: strchr
功 能: 在一个串中查找给定字符的第一个匹配之处
用 法: char *strchr(char *str, char c);
程序例:
#include
#include
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
printf("The character %c is at position: %dn", c, ptr-string);
else
printf("The character was not foundn");
return 0;
}
函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0
程序例:
#include
#include
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
else
printf("buffer 2 is less than buffer 1n");
ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3n");
else
printf("buffer 2 is less than buffer 3n");
return 0;
}
函数名: strncmpi
功 能: 将一个串中的一部分与另一个串比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strcpy
功 能: 串拷贝
用 法: char *strcpy(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strcpy(string, str1);
printf("%sn", string);
return 0;
}
函数名: strcspn
功 能: 在串中查找第一个给定字符集内容的段
用 法: int strcspn(char *str1, char *str2);
程序例:
#include
#include
#include
int main(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";
int length;
length = strcspn(string1, string2);
printf("Character where strings intersect is at position %dn", length);
return 0;
}
函数名: strdup
功 能: 将串拷贝到新建的位置处
用 法: char *strdup(char *str);
程序例:
#include
#include
#include
int main(void)
{
char *dup_str, *string = "abcde";
dup_str = strdup(string);
printf("%sn", dup_str);
free(dup_str);
return 0;
}
函数名: stricmp
功 能: 以大小写不敏感方式比较两个串
用 法: int stricmp(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = stricmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strerror
功 能: 返回指向错误信息字符串的指针
用 法: char *strerror(int errnum);
程序例:
#include
#include
int main(void)
{
char *buffer;
buffer = strerror(errno);
printf("Error: %sn", buffer);
return 0;
}
函数名: strcmpi
功 能: 将一个串与另一个比较, 不管大小写
用 法: int strcmpi(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strncmp
功 能: 串比较
用 法: int strncmp(char *str1, char *str2, int maxlen);
程序例:
#include
#include
int main(void)
{
char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
int ptr;
ptr = strncmp(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
else
printf("buffer 2 is less than buffer 1n");
ptr = strncmp(buf2,buf3,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3n");
else
printf("buffer 2 is less than buffer 3n");
return(0);
}
函数名: strncmpi
功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr = strncmpi(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strncpy
功 能: 串拷贝
用 法: char *strncpy(char *destin, char *source, int maxlen);
程序例:
#include
#include
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strncpy(string, str1, 3);
string[3] = '';
printf("%sn", string);
return 0;
}
函数名: strnicmp
功 能: 不注重大小写地比较两个串
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr = strnicmp(buf2, buf1, 3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strnset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strnset(char *str, char ch, unsigned n);
程序例:
#include
#include
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';
printf("string before strnset: %sn", string);
strnset(string, letter, 13);
printf("string after strnset: %sn", string);
return 0;
}
函数名: strpbrk
功 能: 在串中查找给定字符集中的字符
用 法: char *strpbrk(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *string1 = "abcdefghijklmnopqrstuvwxyz";
char *string2 = "onm";
char *ptr;
ptr = strpbrk(string1, string2);
if (ptr)
printf("strpbrk found first character: %cn", *ptr);
else
printf("strpbrk didn't find character in setn");
return 0;
}
函数名: strrchr
功 能: 在串中查找指定字符的最后一个出现
用 法: char *strrchr(char *str, char c);
程序例:
#include
#include
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strrchr(string, c);
if (ptr)
printf("The character %c is at position: %dn", c, ptr-string);
else
printf("The character was not foundn");
return 0;
}
函数名: strrev
功 能: 串倒转
用 法: char *strrev(char *str);
程序例:
#include
#include
int main(void)
{
char *forward = "string";
printf("Before strrev(): %sn", forward);
strrev(forward);
printf("After strrev(): %sn", forward);
return 0;
}
函数名: strset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strset(char *str, char c);
程序例:
#include
#include
int main(void)
{
char string[10] = "123456789";
char symbol = 'c';
printf("Before strset(): %sn", string);
strset(string, symbol);
printf("After strset(): %sn", string);
return 0;
}
函数名: strspn
功 能: 在串中查找指定字符集的子集的第一次出现
用 法: int strspn(char *str1, char *str2);
程序例:
#include
#include
#include
int main(void)
{
char *string1 = "1234567890";
char *string2 = "123DC8";
int length;
length = strspn(string1, string2);
printf("Character where strings differ is at position %dn", length);
return 0;
}
函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %sn", ptr);
return 0;
}
函数名: strtod
功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例:
#include
#include
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lfn", input, value);
return 0;
}
函数名: strtok
功 能: 查找由在第二个串中指定的分界符分隔开的单词
用 法: char *strtok(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char input[16] = "abc,d";
char *p;
/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%sn", p);
/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%sn", p);
return 0;
}
函数名: strtol
功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
程序例:
#include
#include
int main(void)
{
char *string = "87654321", *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 10);
printf("string = %s long = %ldn", string, lnumber);
return 0;
}
函数名: strupr
功 能: 将串中的小写字母转换为大写字母
用 法: char *strupr(char *str);
程序例:
#include
#include
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
/* converts string to upper case characters */
ptr = strupr(string);
printf("%sn", ptr);
return 0;
}
函数名: swab
功 能: 交换字节
用 法: void swab (char *from, char *to, int nbytes);
程序例:
#include
#include
#include
char source[15] = "rFna koBlrna d";
char target[15];
int main(void)
{
swab(source, target, strlen(source));
printf("This is target: %sn", target);
return 0;
}
PS:isalpha()是字符函数,不是字符串函数,
isalpha
原型:extern int isalpha(int c);
用法:#include
功能:判断字符c是否为英文字母
说明:当c为英文字母a-z或A-Z时,返回非零值,否则返回零。
举例:
// isalpha.c
#include
#include
#include
main()
{
int c;
clrscr(); // clear screen
printf("Press a key");
for(;;)
{
c=getchar();
clrscr();
printf("%c: %s letter",c,isalpha(c)?"is":"not");
}
return 0; // just to avoid warnings by compiler
}
C库函数中strrchr函数的定义是什么?
其实那个叫”实现“,不叫定义。定义是标准干的事。
各个C运行时库(C Library)有不同的实现(implementation)。
这里列举的是uClibc和glibc。
微软Visual C++ 的C库是不开源滴。
函数原型(定义):
extern char * strrchr (const char *s, int c);uClibc 实现:(string/strrchr.c)
/* * Copyright (C) 2002 Manuel Novoa III * Copyright (C) 2000-2005 Erik Andersen
* * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. */#include "_string.h"#ifdef WANT_WIDE# define Wstrrchr wcsrchr#else# define Wstrrchr strrchr#endifWchar *Wstrrchr(register const Wchar *s, Wint c){ register const Wchar *p; p = NULL; do { if (*s == (Wchar) c) { p = s; } } while (*s++); return (Wchar *) p; /* silence the warning */}#ifndef WANT_WIDElibc_hidden_weak(strrchr)# ifdef __UCLIBC_SUSV3_LEGACY__weak_alias(strrchr,rindex)# endif#endifGNU libc 实现:(string/strrchr.c)
/* Copyright (C) 1991-2014 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see <*/#include
#undef strrchr/* Find the last occurrence of C in S. */char *strrchr (const char *s, int c){ const char *found, *p; c = (unsigned char) c; /* Since strchr is fast, we use it rather than the obvious loop. */ if (c == '\0') return strchr (s, '\0'); found = NULL; while ((p = strchr (s, c)) != NULL) { found = p; s = p + 1; } return (char *) found;}#ifdef weak_alias#undef rindexweak_alias (strrchr, rindex)#endiflibc_hidden_builtin_def (strrchr)
c语言string的用法大全
C语言是一门面向过程的、抽象化的通用程序设计语言,广泛应用于底层开发。C语言能以简易的方式编译、处理低级存储器。C 语言string的用法有哪些呢,请看看下面我为你整理 总结 的c语言string的用法大全_C语言中string使用 方法 。
c语言string的用法
函数原型:char *strdup(const char *s)
函数功能:字符串拷贝,目的空间由该函数分配
函数返回:指向拷贝后的字符串指针
参数说明:src-待拷贝的源字符串
所属文件:
[cpp] view plain
#include
#include
#include
intmain()
{
char*dup_str,*string="abcde";
dup_str=strdup(string);
printf("%s",dup_str);
free(dup_str);
return0;
}
@函数名称:strcpy
函数原型:char* strcpy(char* str1,char* str2);
函数功能:把str2指向的字符串拷贝到str1中去
函数返回:返回str1,即指向str1的指针
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
charstring[10];
char*str1="abcdefghi";
strcpy(string,str1);
printf("thestringis:%s\n",string);
return0;
}
@函数名称:strncpy
函数原型:char *strncpy(char *dest, const char *src,intcount)
函数功能:将字符串src中的count个字符拷贝到字符串dest中去
函数返回:指向dest的指针
参数说明:dest-目的字符串,src-源字符串,count-拷贝的字符个数
所属文件:
[cpp] view plain
#include
#include
intmain()
{
char*src="bbbbbbbbbbbbbbbbbbbb";//20'b's
chardest[50]="aaaaaaaaaaaaaaaaaaaa";//20'a's
puts(dest);
strncpy(dest,src,10);
puts(dest);
return0;
}
输出:
[cpp] view plain
/*******************************************
aaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbaaaaaaaaaa
*******************************************/
注意:strncpy只复制指定长度的字符,不会自动在末尾加'\0'。若指定长度超过源字符串长度,不够的部分补‘\0’,
@函数名称:strcat
函数原型:char* strcat(char * str1,char * str2);
函数功能:把字符串str2接到str1后面,str1最后的'\0'被取消
函数返回:str1
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
charbuffer[80];
strcpy(buffer,"Hello");
strcat(buffer,"world");
printf("%s\n",buffer);
return0;
}
@函数名称:strncat
函数原型:char *strncat(char *dest, const char *src, size_t maxlen)
函数功能:将字符串src中前maxlen个字符连接到dest中
函数返回:
参数说明:
所属文件:
[cpp] view plain
#include
#include
charbuffer[80];
intmain()
{
strcpy(buffer,"Hello");
strncat(buffer,"world",8);
printf("%s\n",buffer);
strncat(buffer,"*************",4);
printf("%s\n",buffer);
return0;
}
注意:与strncpy不同的是,strncat会自动在末尾加‘\0’,若指定长度超过源字符串长度,则只复制源字符串长度即停止
@函数名称:strcmp
函数原型:int strcmp(char * str1,char * str2);
函数功能:比较两个字符串str1,str2.
函数返回:str1
str2,返回正数.
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
char*buf1="aaa",*buf2="bbb",*buf3="ccc";
intptr;
ptr=strcmp(buf2,buf1);
if(ptr>0)
printf("buffer2isgreaterthanbuffer1\n");
else
printf("buffer2islessthanbuffer1\n");
ptr=strcmp(buf2,buf3);
if(ptr>0)
printf("buffer2isgreaterthanbuffer3\n");
else
printf("buffer2islessthanbuffer3\n");
return0;
}
@函数名称:strncmp
函数原型:int strncmp(char *str1,char *str2,int count)
函数功能:对str1和str2中的前count个字符按字典顺序比较
函数返回:小于0:str1
str2
参数说明:str1,str2-待比较的字符串,count-比较的长度
所属文件:
[cpp] view plain
#include
#include
intmain()
{
charstr1[]="aabbc";//
charstr2[]="abbcd";//
//为使测试程序更简练,此处假定了strncmp只返回-1,0,1三个数
charres_info[]={'<','=','>'};
intres;
//前1个字符比较
res=strncmp(str1,str2,1);
printf("1:str1%cstr2\n",res_info[res+1]);
//前3个字符比较
res=strncmp(str1,str2,3);
printf("3:str1%cstr2\n",res_info[res+1]);
}
输出:
[cpp] view plain
/****************************************
1:str1=str2
3:str1
<str2
*****************************************/
@函数名称:strpbrk
函数原型:char *strpbrk(const char *s1, const char *s2)
函数功能:得到s1中第一个“同时也出现在s2中”字符的位置指针
函数返回:位置指针
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
char*p="Findallvowels";
p=strpbrk(p+1,"aeiouAEIOU");
while(p)
{
printf("%s\n",p);
p=strpbrk(p+1,"aeiouAEIOU");
}
return0;
}
输出:
[cpp] view plain
/**************************************
indallvowels
allvowels
owels
els
**************************************/
@函数名称:strcspn
函数原型:int strcspn(const char *s1, const char *s2)
函数功能:统计s1中从头开始直到第一个“来自s2中的字符”出现的长度
函数返回:长度
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
printf("%d\n",strcspn("abcbcadef","cba"));
printf("%d\n",strcspn("xxxbcadef","cba"));
printf("%d\n",strcspn("123456789","cba"));
return0;
}
输出:
[cpp] view plain
/************************
0
3
9
************************/
@函数名称:strspn
函数原型:int strspn(const char *s1, const char *s2)
函数功能:统计s1中从头开始直到第一个“不来自s2中的字符”出现的长度
函数返回:位置指针
参数说明:
所属文件:
[html] view plain
#include
#include
#include
intmain()
{
printf("%d\n",strspn("abcbcadef","cba"));
printf("%d\n",strspn("xxxbcadef","cba"));
printf("%d\n",strspn("123456789","cba"));
return0;
}
输出:
[cpp] view plain
/************************
6
0
0
************************/
@函数名称:strchr
函数原型:char* strchr(char* str,char ch);
函数功能:找出str指向的字符串中第一次出现字符ch的位置
函数返回:返回指向该位置的指针,如找不到,则返回空指针
参数说明:str-待搜索的字符串,ch-查找的字符
所属文件:
[cpp] view plain
#include
#include
intmain()
{
char*str="Thisisastring!";
charch;
char*p;
while(1)
{
printf("Pleaseinputachar:");
ch=getchar();
p=strchr(str,ch);
if(p)
printf("%cisthe%dcharacterof\"%s\"\n",ch,(int)(p-str+1),str);
else
printf("Notfound!\n");
printf("PressESCtoquit!\n\n");
if(27==getch())
break;
fflush(stdin);
}
return0;
}
运行结果:
[cpp] view plain
/********************************************
Pleaseinputachar:i
iisthe3characterof"Thisisastring!"
PressESCtoquit!
Pleaseinputachar:l
Notfound!
PressESCtoquit!
Pleaseinputachar:s
sisthe4characterof"Thisisastring!"
PressESCtoquit!
**********************************************/
@函数名称:strrchr
函数原型:char *strrchr(const char *s, int c)
函数功能:得到字符串s中最后一个含有c字符的位置指针
函数返回:位置指针
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
charstring[15];
char*ptr,c='r';
strcpy(string,"Thisisastring");
ptr=strrchr(string,c);
if(ptr)
printf("Thecharacter%cisatposition:%d",c,ptr-string);
else
printf("Thecharacterwasnotfound");
return0;
}
@函数名称:strstr
函数原型:char* strstr(char* str1,char* str2);
函数功能:找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)
函数返回:返回该位置的指针,如找不到,返回空指针
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
char*str1="OpenWatcomC/C++",*str2="Watcom",*ptr;
ptr=strstr(str1,str2);
printf("Thesubstringis:%s\n",ptr);
return0;
}
输出:
The substringis:Watcom C/C++
@函数名称:strrev
函数原型:char *strrev(char *s)
函数功能:将字符串中的所有字符颠倒次序排列
函数返回:指向s的指针
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
charforward[]="string";//原文中定义为char*是不对的,指向代码段的指针内容是不可变的
printf("Beforestrrev():%s",forward);
strrev(forward);
printf("Afterstrrev():%s",forward);
return0;
}
输出:
[cpp] view plain
/************************************
Beforestrrev():string
Afterstrrev():gnirts
************************************/
@函数名称:strnset
函数原型:char *strnset(char *s, int ch, size_t n)
函数功能:将字符串s中前n个字符设置为ch的值
函数返回:指向s的指针
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
charstring[]="aaaaaaaaaaaaaaaaaaaaaaa";
charletter='x';
printf("stringbeforestrnset:%s\n",string);
strnset(string,letter,10);
printf("stringafterstrnset:%s\n",string);
return0;
}
输出:
[cpp] view plain
/*************************************************
stringbeforestrnset:aaaaaaaaaaaaaaaaaaaaaaa
stringafterstrnset:xxxxxxxxxxaaaaaaaaaaaaa
*************************************************/
@函数名称:strset
函数原型:char *strset(char *s, int ch)
函数功能:将字符串s中所有字符设置为ch的值
函数返回:指向s的指针
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
charstring[10]="123456789";
charsymbol='c';
printf("Beforestrset():%s",string);
strset(string,symbol);
printf("Afterstrset():%s",string);
return0;
}
@函数名称:strtok
函数原型:char *strtok(char *s1, const char *s2)
函数功能:分解s1字符串为用特定分隔符分隔的多个字符串(一般用于将英文句分解为单词)
函数返回:字符串s1中首次出现s2中的字符前的子字符串指针
参数说明:s2一般设置为s1中的分隔字符
规定进行子调用时(即分割s1的第二、三及后续子串)第一参数必须是NULL
在每一次匹配成功后,将s1中分割出的子串位置替换为NULL(摘下链中第一个环),因此s1被破坏了
函数会记忆指针位置以供下一次调用
所属文件:
[cpp] view plain
#include
#include
intmain()
{
char*p;
char*buffer;
char*delims={".,"};
buffer=strdup("Findwords,allofthem.");
printf("%s\n",buffer);
p=strtok(buffer,delims);
while(p!=NULL){
printf("word:%s\n",p);
p=strtok(NULL,delims);
}
printf("%s\n",buffer);
return0;
}//根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字符串的分隔
PS:根据测试,可以随时给strtok的第一个参数输入一个新的字符串,开始新字符串的分隔
@函数名称:strupr
函数原型:char *strupr(char *s)
函数功能:将字符串s中的字符变为大写
函数返回:
参数说明:
所属文件:
[cpp] view plain
#include
#include
intmain()
{
charstring[]="abcdefghijklmnopqrstuvwxyz",*ptr;//会影响原字符串的内存,用char[]来声明
ptr=strupr(string);
printf("%s",ptr);
return0;
}
@函数名称:strlwr
函数原型:char *strlwr(char *s)
函数功能:将字符串中的字符变为小写字符
函数返回:指向s的指针
参数说明:
所属文件:
[cpp] view plain
#include
intmain()
{
charstr[]="HOWTOSAY";
printf("%s",strlwr(str));
return0;
}
@函数名称:strerror
函数原型:char *strerror(int errnum)
函数功能:得到错误信息的内容信息
函数返回:错误提示信息字符串指针
参数说明:errnum-错误编号
所属文件:
[cpp] view plain
#include
#include
intmain()
{
char*buffer;
buffer=strerror(errno);
printf("Error:%s",buffer);
return0;
}
@函数名称:memcpy
函数原型:void *memcpy(void *dest, const void *src, size_t n)
函数功能:字符串拷贝
函数返回:指向dest的指针
参数说明:src-源字符串,n-拷贝的最大长度
所属文件:
,
[cpp] view plain
#include
#include
intmain()
{
charsrc[]="******************************";
chardest[]="abcdefghijlkmnopqrstuvwxyz0123456709";
char*ptr;
printf("destinationbeforememcpy:%s\n",dest);
ptr=memcpy(dest,src,strlen(src));
if(ptr)
printf("destinationaftermemcpy:%s\n",dest);
else
printf("memcpyfailed");
return0;
}
输出:
[cpp] view plain
/*************************************************************
destinationbeforememcpy:abcdefghijlkmnopqrstuvwxyz0123456709
destinationaftermemcpy:******************************456709
**************************************************************/
@函数名称:memccpy
函数原型:void *memccpy(void *dest, const void *src, int c, size_t n)
函数功能:字符串拷贝,到指定长度或遇到指定字符时停止拷贝
函数返回:
参数说明:src-源字符串指针,c-中止拷贝检查字符,n-长度,dest-拷贝底目的字符串指针
所属文件:
,
[cpp] view plain
#include
#include
intmain()
{
char*src="Thisisthesourcestring";
chardest[50];
char*ptr;
ptr=memccpy(dest,src,'c',strlen(src));
if(ptr)
{
*ptr='\0';
printf("Thecharacterwasfound:%s",dest);
}
else
printf("Thecharacterwasn'tfound");
return0;
}
输出:
[cpp] view plain
/*****************************************
Thecharacterwasfound:Thisisthesourc
*****************************************/
PS:指定字符被复制到dest中,memccpy返回了dest中指定字符的下一处的地址,返回NULL表示未遇到指定字符
@函数名称:memchr
函数原型:void *memchr(const void *s, int c, size_t n)
函数功能:在字符串中第开始n个字符中寻找某个字符c的位置
函数返回:返回c的位置指针,返回NULL时表示未找到
参数说明:s-要搜索的字符串,c-要寻找的字符,n-指定长度
所属文件:
,
[cpp] view plain
#include
#include
intmain()
{
charstr[17];
char*ptr;
strcpy(str,"Thisisastring");
ptr=memchr(str,'r',strlen(str));
if(ptr)
printf("Thecharacter'r'isatposition:%d",ptr-str);
else
printf("Thecharacterwasnotfound");
return0;
}
@函数名称:memcmp
函数原型:int memcmp(const void *s1, const void *s2,size_t n)
函数功能:按字典顺序比较两个串s1和s2的前n个字节
函数返回:<0,=0,>0分别表示s1<,=,>s2
参数说明:s1,s2-要比较的字符串,n-比较的长度
所属文件:
,
[cpp] view plain
#include
#include
intmain()
{
char*buf1="ABCDE123";
char*buf2="abcde456";
intstat;
stat=memcmp(buf1,buf2,5);
printf("Thestringstoposition5are");
if(stat)printf("not");
printf("thesame\n");
return0;
}
@函数名称:memicmp
函数原型:int memicmp(const void *s1, const void *s2, size_t n)
函数功能:按字典顺序、不考虑字母大小写对字符串s1,s2前n个字符比较
函数返回:<0,=0,>0分别表示s1<,=,>s2
参数说明:s1,s2-要比较的字符串,n-比较的长度
所属文件:
,
[cpp] view plain
#include
#include
intmain()
{
char*buf1="ABCDE123";
char*buf2="abcde456";
intstat;
stat=memicmp(buf1,buf2,5);
printf("Thestringstoposition5are");
if(stat)printf("not");
printf("thesame");
return0;
}
输出:
[cpp] view plain
/**************************************
Thestringstoposition5arethesame
***************************************/
@函数名称:memmove
函数原型:void *memmove(void *dest, const void *src, size_t n)
函数功能:字符串拷贝
函数返回:指向dest的指针
参数说明:src-源字符串,n-拷贝的最大长度
所属文件:
,
[cpp] view plain
#include
#include
intmain()
{
chardest[40]="abcdefghijklmnopqrstuvwxyz0123456789";
printf("destinationpriortomemmove:%s\n",dest);
memmove(dest+1,dest,35);
printf("destinationaftermemmove:%s",dest);
return0;
}
PS:与memcpy不同的是,memmove可以处理目的字符串与源字符串地址空间出现重叠的情况,可保证待复制的内容不被破坏。
@函数名称: memset
函数原型: void *memset(void *s, int c, size_t n)
函数功能: 字符串中的n个字节内容设置为c
函数返回:
参数说明: s-要设置的字符串,c-设置的内容,n-长度
所属文件:
,
[cpp] view plain
#include
#include
#include
intmain()
{
charbuffer[]="Helloworld";
printf("Bufferbeforememset:%s/n",buffer);
memset(buffer,'*',strlen(buffer)-1);
printf("Bufferaftermemset:%s",buffer);
return0;
}
c语言string的用法大全相关 文章 :
c语言string的用法
c语言的用法
Linux C语言字符与字符串处理
c语言中strcmp的用法
c语言大括号的用法
c语言位运算符的用法
c语言char的用法
c语言中sort的用法详解
c语言中int的用法
c语言map的用法
</str2
求strcmp c++中的用法详细说明
简单点说,就是strcmp中两个参数的对比.
这个函数可以比较基本类型的变量.
例如,
char a='A',b='B';
if(strcmp(a,b)>0) cout<<"a>b"<
<endl;
if(strcmp(a,b)<0) cout<<"a
<b"<<endl;
if(strcmp(a,b)==0) cout<<"a=b"<
<endl;
如上,用法就是这样,lz应该一看就明白了吧.
函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0
程序例:
#include
#include
int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
else
printf("buffer 2 is less than buffer 1n");
ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3n");
else
printf("buffer 2 is less than buffer 3n");
return 0;
}
/*下面再给你其他相关的函数应用*/
函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
程序例:
#include
#include
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
printf("%sn", string);
return 0;
}
函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例:
#include
#include
int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%sn", destination);
return 0;
}
函数名: strchr
功 能: 在一个串中查找给定字符的第一个匹配之处
用 法: char *strchr(char *str, char c);
程序例:
#include
#include
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
printf("The character %c is at position: %dn", c, ptr-string);
else
printf("The character was not foundn");
return 0;
}
函数名: strncmpi
功 能: 将一个串中的一部分与另一个串比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2, unsigned maxlen);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strcpy
功 能: 串拷贝
用 法: char *strcpy(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strcpy(string, str1);
printf("%sn", string);
return 0;
}
函数名: strcspn
功 能: 在串中查找第一个给定字符集内容的段
用 法: int strcspn(char *str1, char *str2);
程序例:
#include
#include
#include
int main(void)
{
char *string1 = "1234567890";
char *string2 = "747DC8";
int length;
length = strcspn(string1, string2);
printf("Character where strings intersect is at position %dn", length);
return 0;
}
函数名: strdup
功 能: 将串拷贝到新建的位置处
用 法: char *strdup(char *str);
程序例:
#include
#include
#include
int main(void)
{
char *dup_str, *string = "abcde";
dup_str = strdup(string);
printf("%sn", dup_str);
free(dup_str);
return 0;
}
函数名: stricmp
功 能: 以大小写不敏感方式比较两个串
用 法: int stricmp(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = stricmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strerror
功 能: 返回指向错误信息字符串的指针
用 法: char *strerror(int errnum);
程序例:
#include
#include
int main(void)
{
char *buffer;
buffer = strerror(errno);
printf("Error: %sn", buffer);
return 0;
}
函数名: strcmpi
功 能: 将一个串与另一个比较, 不管大小写
用 法: int strcmpi(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBB", *buf2 = "bbb";
int ptr;
ptr = strcmpi(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strncmp
功 能: 串比较
用 法: int strncmp(char *str1, char *str2, int maxlen);
程序例:
#include
#include
int main(void)
{
char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";
int ptr;
ptr = strncmp(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
else
printf("buffer 2 is less than buffer 1n");
ptr = strncmp(buf2,buf3,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3n");
else
printf("buffer 2 is less than buffer 3n");
return(0);
}
函数名: strncmpi
功 能: 把串中的一部分与另一串中的一部分比较, 不管大小写
用 法: int strncmpi(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr = strncmpi(buf2,buf1,3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strncpy
功 能: 串拷贝
用 法: char *strncpy(char *destin, char *source, int maxlen);
程序例:
#include
#include
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strncpy(string, str1, 3);
string[3] = '';
printf("%sn", string);
return 0;
}
函数名: strnicmp
功 能: 不注重大小写地比较两个串
用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);
程序例:
#include
#include
int main(void)
{
char *buf1 = "BBBccc", *buf2 = "bbbccc";
int ptr;
ptr = strnicmp(buf2, buf1, 3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1n");
if (ptr < 0)
printf("buffer 2 is less than buffer 1n");
if (ptr == 0)
printf("buffer 2 equals buffer 1n");
return 0;
}
函数名: strnset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strnset(char *str, char ch, unsigned n);
程序例:
#include
#include
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';
printf("string before strnset: %sn", string);
strnset(string, letter, 13);
printf("string after strnset: %sn", string);
return 0;
}
函数名: strpbrk
功 能: 在串中查找给定字符集中的字符
用 法: char *strpbrk(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *string1 = "abcdefghijklmnopqrstuvwxyz";
char *string2 = "onm";
char *ptr;
ptr = strpbrk(string1, string2);
if (ptr)
printf("strpbrk found first character: %cn", *ptr);
else
printf("strpbrk didn't find character in setn");
return 0;
}
函数名: strrchr
功 能: 在串中查找指定字符的最后一个出现
用 法: char *strrchr(char *str, char c);
程序例:
#include
#include
int main(void)
{
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strrchr(string, c);
if (ptr)
printf("The character %c is at position: %dn", c, ptr-string);
else
printf("The character was not foundn");
return 0;
}
函数名: strrev
功 能: 串倒转
用 法: char *strrev(char *str);
程序例:
#include
#include
int main(void)
{
char *forward = "string";
printf("Before strrev(): %sn", forward);
strrev(forward);
printf("After strrev(): %sn", forward);
return 0;
}
函数名: strset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strset(char *str, char c);
程序例:
#include
#include
int main(void)
{
char string[10] = "123456789";
char symbol = 'c';
printf("Before strset(): %sn", string);
strset(string, symbol);
printf("After strset(): %sn", string);
return 0;
}
函数名: strspn
功 能: 在串中查找指定字符集的子集的第一次出现
用 法: int strspn(char *str1, char *str2);
程序例:
#include
#include
#include
int main(void)
{
char *string1 = "1234567890";
char *string2 = "123DC8";
int length;
length = strspn(string1, string2);
printf("Character where strings differ is at position %dn", length);
return 0;
}
函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %sn", ptr);
return 0;
}
函数名: strtod
功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例:
#include
#include
int main(void)
{
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lfn", input, value);
return 0;
}
函数名: strtok
功 能: 查找由在第二个串中指定的分界符分隔开的单词
用 法: char *strtok(char *str1, char *str2);
程序例:
#include
#include
int main(void)
{
char input[16] = "abc,d";
char *p;
/* strtok places a NULL terminator
in front of the token, if found */
p = strtok(input, ",");
if (p) printf("%sn", p);
/* A second call to strtok using a NULL
as the first parameter returns a pointer
to the character following the token */
p = strtok(NULL, ",");
if (p) printf("%sn", p);
return 0;
}
函数名: strtol
功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
程序例:
#include
#include
int main(void)
{
char *string = "87654321", *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 10);
printf("string = %s long = %ldn", string, lnumber);
return 0;
}
函数名: strupr
功 能: 将串中的小写字母转换为大写字母
用 法: char *strupr(char *str);
程序例:
#include
#include
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
/* converts string to upper case characters */
ptr = strupr(string);
printf("%sn", ptr);
return 0;
}
函数名: swab
功 能: 交换字节
用 法: void swab (char *from, char *to, int nbytes);
程序例:
#include
#include
#include
char source[15] = "rFna koBlrna d";
char target[15];
int main(void)
{
swab(source, target, strlen(source));
printf("This is target: %sn", target);
return 0;
}
PS:isalpha()是字符函数,不是字符串函数,
isalpha
原型:extern int isalpha(int c);
用法:#include
功能:判断字符c是否为英文字母
说明:当c为英文字母a-z或A-Z时,返回非零值,否则返回零。
举例:
// isalpha.c
#include
#include
#include
main()
{
int c;
clrscr(); // clear screen
printf("Press a key");
for(;;)
{
c=getchar();
clrscr();
printf("%c: %s letter",c,isalpha(c)?"is":"not");
}
return 0; // just to avoid warnings by compiler
}
</endl;
</b"<<endl;
</endl;
strchr函数第二个参数怎么是整型的? ‘s' 是一个字符,但他是什么类型的呢?
第二个参数,是字符型,如果传了整型的参数,而没有出错,估计被强制转换为字符型了,不建议这样做
C中字符转整型是很常有的, char实际上就是一字节的整数嘛, 's' 的ASCII码是115, 调用时是传一个整数115给第二个参数的.
char*strchr(constchar*string,intc);
Example
/* STRCHR.C: This program illustrates searching for a character
* with strchr (search forward) or strrchr (search backward).
*/
#include
#include
int ch = 'r';
char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";
void main( void )
{
char *pdest;
int result;
printf( "String to be searched: \n\t\t%s\n", string );
printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
printf( "Search char:\t%c\n", ch );
/* Search forward. */
pdest = strchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tfirst %c found at position %d\n\n",
ch, result );
else
printf( "Result:\t%c not found\n" );
/* Search backward. */
pdest = strrchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tlast %c found at position %d\n\n", ch, result );
else
printf( "Result:\t%c not found\n" );
}
Output
String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890
Search char: r
Result: first r found at position 12
Result: last r found at position 30
这个是历史原因,很久以前C语言是没有函数原型的,差不多就是函数声明只有名字没有参数,并且无论什么整数类型的传递到函数之前都会自动转换成int,也就是说char, short之类的都会转换成int,于是就定义成int。现在的C语言已经不必这样了,但是为了兼容性,这个带int函数的原型就保留了下来。里面是会强制转换成char的。
C语言中对字符串进行操作的标准库函数有哪些
1)字符串操作
strcpy(p, p1) 复制字符串
strncpy(p, p1, n) 复制指定长度字符串
strcat(p, p1) 附加字符串
strncat(p, p1, n) 附加指定长度字符串
strlen(p) 取字符串长度
strcmp(p, p1) 比较字符串
strcasecmp忽略大小写比较字符串
strncmp(p, p1, n) 比较指定长度字符串
strchr(p, c) 在字符串中查找指定字符
strrchr(p, c) 在字符串中反向查找
strstr(p, p1) 查找字符串
strpbrk(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找该集合的任一元素
strspn(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找不属于该集合的任一元素的偏移
strcspn(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找属于该集合的任一元素的偏移
* 具有指定长度的字符串处理函数在已处理的字符串之后填补零结尾符
2)字符串到数值类型的转换
strtod(p, ppend) 从字符串 p 中转换 double 类型数值,并将后续的字符串指针存储到 ppend 指向的 char* 类型存储。
strtol(p, ppend, base) 从字符串 p 中转换 long 类型整型数值,base 显式设置转换的整型进制,设置为 0 以根据特定格式判断所用进制,0x, 0X 前缀以解释为十六进制格式整型,0 前缀以解释为八进制格式整型
atoi(p) 字符串转换到 int 整型
atof(p) 字符串转换到 double 符点数
atol(p) 字符串转换到 long 整型
3)字符检查
isalpha() 检查是否为字母字符
isupper() 检查是否为大写字母字符
islower() 检查是否为小写字母字符
isdigit() 检查是否为数字
isxdigit() 检查是否为十六进制数字表示的有效字符
isspace() 检查是否为空格类型字符
iscntrl() 检查是否为控制字符
ispunct() 检查是否为标点符号
isalnum() 检查是否为字母和数字
isprint() 检查是否是可打印字符
isgraph() 检查是否是图形字符,等效于 isalnum() | ispunct()
怎样用c语言制作电子印章
BOOL COkMfcView::exSaveBufferFile(HANDLE hBoard, HWND hWnd){char szFileName[128]="";BLOCKINFO blkinfo;CSeqNum csf;ZeroMemory(&blkinfo,sizeof(BLOCKINFO));if (!exGetSaveName(hWnd, szFileName)) return FALSE;if (!strrchr(szFileName, '.')) return FALSE;if(!strnicmp(strrchr(szFileName,'.'), ".seq", 4) ||!strnicmp(strrchr(szFileName,'.'), ".avi", 4) ||!strnicmp(strrchr(szFileName,'.'), ".m2v", 4)) {//save buffer seqtotal = (short)okGetBufferSize(hBoard, NULL, NULL);okSaveImageFile(hBoard, szFileName, 0, BUFFER, 0, total);//than set this first block to dib to see what we savedexGetBitmapHeader(hBoard, BUFFER, lpbi); exSetDataToDIB(BUFFER, 0, lpbi, (LPBYTE)lpdib);}else //bmp or bmp{if (okGetBufferSize(hBoard, NULL, (DWORD*)&dwBufSize)) //buffer being{long lNum = 1;//how manyif (!csf.DoModal()) return FALSE;lNum = iNumImage;okSaveImageFile(hBoard,szFileName,1,BUFFER,0,lNum);//than set this first block to dib to see what we savedexGetBitmapHeader(hBoard, BUFFER, lpbi); exSetDataToDIB(BUFFER, 0, lpbi, (LPBYTE)lpdib);}else //no buffer allocated{//save current dibblkinfo.iWidth = (short)lpbi->biWidth;blkinfo.iHeight = (short)lpbi->biHeight;blkinfo.lBlockStep=0;blkinfo.iHiStep=0;blkinfo.iBitCount = (short)lpbi->biBitCount;blkinfo.lpBits = (LPBYTE)lpdib;blkinfo.lpExtra = (LPBYTE)((LPSTR)lpbi+lpbi->biSize);blkinfo.iFormType = 0; //unknown//special for 565if (lpbi->biBitCount == 16) {DWORD *lpMask=(DWORD *)blkinfo.lpExtra;if (lpMask[1] == 0x07e0) //565blkinfo.iFormType = FORM_RGB565;elseblkinfo.iFormType = FORM_RGB555;}//first inverse dib to bitmapexConvertBitmap(lpbi, lpdib);okSaveImageFile(hBoard, szFileName, 0, (TARGET)&blkinfo, 0, 1);//restore to dibexConvertBitmap(lpbi, lpdib);}}return TRUE;}
1、用C语言非常麻烦,可以下载软件进行制作。 2、百度搜索电子印章软件。 3、打开网页后点击普通安装。
4、双击打开软件。
5、进行制作。
6、点击文本输入印章内容调整下面成熟。
7、制作完毕可以导出或者复制到剪贴板等。