C语言字符串操作函数
引用自:
http://www.cnblogs.com/JCSU/articles/1305401.html
1. 字符串反转 - strRev
2. 字符串复制 - strcpy
3. 字符串转化为整数 - atoi
4. 字符串求长 - strlen
5. 字符串连接 - strcat
6. 字符串比较 - strcmp
7. 计算字符串中的元音字符个数
8. 判断一个字符串是否是回文
1. 写一个函数实现字符串反转
版本1 - while版
{
char temp, *end = s + strlen(s) - 1;
while( end > s)
{
temp = *s;
*s = *end;
*end = temp;
--end;
++s;
}
}
版本2 - for版
{
char temp;
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
temp = *s;
*s = *end;
*end = temp;
}
}
版本3 - 不使用第三方变量
{
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
*s ^= *end;
*end ^= *s;
*s ^= *end;
}
}
版本4 - 重构版本3
{
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
*s ^= *end ^= *s ^= *end;
}
}
版本5 - 重构版本4
{
for(char *end = s + strlen(s) - 1; end > s ; *s++ ^= *end ^= *s ^= *end--);
}
版本6 - 递归版
{
if(s[0] == '\0')
return;
else
strRev(&s[1]);
printf("%c",s[0]);
}
2. 实现库函数strcpy的功能
strcpy函数位于头文件<string.h>中
版本1
{
char *p=dest;
while(*dest++ = *src++)
;
dest=p;
}
版本2
{
char *p = dst;
while( *p ++ = *src ++ )
;
return dst;
}
版本3
{
int i=0;
for(; *(src+i)!='\0'; i++)
*(dest+i) = *(src+i);
*(dest+i) = '\0';
}
3. 实现库函数atoi的功能
atoi函数位于头文件<stdlib.h>中
版本1 - 附说明
{
if( 0 == exp )
return 1;
return base*power(base, exp-1);
}
int __cdecl atoi(const char *s)
{
int exp=0, n=0;
const char *t = NULL;
for(; *s == ' ' || *s == '\t' || *s == '\n'; s++) //找到第一个非空字符
;
if( *s >'9' || *s <'0' ) //如果第一个非空字符不是数字字符,返回0
return 0;
for(t=s; *t >='0' && *t <='9'; ++t) //找到第一个非数字字符位置 - 方法1
;
t--;
/* 找到第一个非数字字符位置 - 方法2
t=s;
while(*t++ >='0' && *t++ <='9')
;
t -= 2;
*/
while(t>=s)
{
n+=(*t - 48)*power(10, exp); //数字字符转化为整数
t--;
exp++;
}
return n;
}
版本2
{
int exp=0, n=0;
const char *t = NULL;
for(; *s == ' ' || *s == '\t' || *s == '\n'; s++) //略过非空字符
;
if( *s >'9' || *s <'0' )
return 0;
for(t=s; *t >='0' && *t <='9'; ++t)
;
t--;
while(t>=s)
{
n+=(*t - 48)*pow(10, exp);
t--;
exp++;
}
return n;
}
4. 实现库函数strlen的功能
strlen函数位于头文件<string.h>中
版本1 - while版
{
int i = 0;
while( *s )
{
i++;
s++;
}
return i;
}
版本2 - for版
{
for(int i = 0; *s; i++, s++)
;
return i;
}
版本3 - 无变量版
{
if(*s == '\0')
return 0;
else
return (strlen(++s) + 1);
}
版本4 - 重构版本3
{
return *s ? (strlen(++s) + 1) : 0;
}
5. 实现库函数strcat的功能
strcat函数位于头文件<string.h>中
版本1 - while版
{
char *p = dst;
while( *p )
p++;
while( *p ++ = *src ++ )
;
return dst;
}
6. 实现库函数strcmp的功能
strcmp函数位于头文件<string.h>中
版本1 - 错误的strcmp
{
for(; *a !='\0' && *b !='\0'; a++, b++)
if( *a > *b)
return 1;
else if ( *a==*b)
return 0;
else
return -1;
}
版本2
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *src)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
7. 计算字符串中元音字符的个数
int is_vowel(char a)
{
switch(a)
{
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
return 1; break;
default:
return 0; break;
}
}
int count_vowel(const char *s)
{
int num;
if(s[0] == '\0')
num = 0;
else
{
if(is_vowel(s[0]))
num = 1 + count_vowel(&s[1]);
else
num = count_vowel(&s[1]);
}
return num;
}
int main()
{
char *s=" AobCd ddudIe";
printf("%d \n", count_vowel(s));
return 0;
}
8. 判断一个字符串是否回文:包含一个单词,或不含空格、标点的短语。如:Madam I'm Adam是回文
版本1
* 程序功能:判断一个单词,或不含空格、标点符号的短语是否为回文(palindrome)
*/
#include <stdio.h>
#include <ctype.h>
int is_palindrome(const char *s)
{
bool is_palindrome=0;
const char *end=s;
if(*end == '\0') /* 如果s为空串,则是回文 */
is_palindrome=1;
while(*end) ++end; /* end指向串s最后一个字符位置 */
--end;
while(s<=end)
{
while(*s==' ' || !isalpha(*s)) /* 略去串s中的非字母字符 */
++s;
while(*end==' ' || !isalpha(*end))
--end;
if(toupper(*s) == toupper(*end)) /* 将s中的字母字符转换为大字进行判断 */
{
++s;
--end;
}
else
{
is_palindrome=0; break;
} /* 在s<=end的条件下,只要出现不相等就判断s不是回文 */
}
if(s>end)
is_palindrome=1;
else
is_palindrome=0;
return (is_palindrome);
}
int main()
{
const char *s ="Madam I' m Adam";
printf("%s %s \n", s, is_palindrome(s) ? "is a palindrome!": "is not a palindrome!");
return 0;
}
有趣的回文:He lived as a devil, eh?
转载于:https://www.cnblogs.com/yuzaipiaofei/archive/2012/03/04/4124252.html
C语言字符串操作函数相关推荐
- C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
C语言字符串操作函数 1. 字符串反转 - strRev 2. 字符串复制 - strcpy 3. 字符串转化为整数 - atoi 4. 字符串求长 - strlen 5. 字符串连接 - strca ...
- c语言ascii字母比较大小,与ASCII码相关的C语言字符串操作函数
C语言toascii()函数:将字符转换成对应的ASCII码头文件: #include 定义函数: int toascii(int c); 函数说明:toascii()会将参数c 转换成7 位的uns ...
- C语言字符串操作函数整理
2019独角兽企业重金招聘Python工程师标准>>> #include<stdio.h> #include<string.h> #include<st ...
- C语言字符串操作函数汇总
1. strcpy函数 语法:strcpy(str1,str2) 功能:将字符串str2复制到字符串str1中,并覆盖str1原始字符串,可以用来为字符串变量赋值. 返回:str1 注意: 1. 字符 ...
- 字符串操作 c语言,C语言字符串操作(示例代码)
C语言字符串操作函数 1.strlen strlen用于求一个C风格字符串的长度,函数原型为 #include size_t strlen(const char *s); 返回值为字符串的长度,当遇到 ...
- C语言常用字符串操作函数大全详解(strstr,strtok,strrchr,strcat,strcmp,strcpy,strerror,strspn,strchr等)
参考:string.h中常用字符串操作函数说明(strstr,strtok,strrchr,strcat,strcmp,strcpy,strerror,strspn,strchr等) 作者:一只青木呀 ...
- 字符串转内存c语言,【一起学C】C语言面试题必考:字符串操作函数,内存操作函数实现...
本帖最后由 奉聪 于 2017-1-25 14:54 编辑 *******前言******* 我数一下,我发了几个帖子,1.2.3? 哈哈哈,没几个哈,感谢吾爱,让我学到了很多,乐于分享,共同进步! ...
- c语言中空格字符怎么表示_C语言中常用的字符串操作函数
作者:陈太浪 出处:https://home.cnblogs.com/u/TomHe789/ C语言中提供了许多的字符串操作函数,常见的字符串操作函数有以下几种: 1.求字符串长度的函数 原型函数:s ...
- C语言的常用字符串操作函数(一)
一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...
最新文章
- PhoenixGo战胜绝艺,腾讯包揽AI围棋大赛前两名
- 【图论专题】单源最短路的综合应用
- conda创建子环境并注册kernel
- 程序世界的秘密(中)
- Unity 代码集锦之图片处理
- 1313 质因数分解 2012年NOIP全国联赛普及组
- SAP云平台cf push命令报错误码44的解决方法
- 《商业智能BI白皮书3.0》正式发布(附下载链接)
- 信息学奥赛一本通 1065:奇数求和 | OpenJudge NOI 1.5 09
- Fence(CF-324F)
- php 获取mysql大小限制_php计算整个mysql数据库大小的方法
- 浅谈CSS3 响应式布局--Media Queries
- 2dpsk调制解调实验matlab_ila抓取数据,matlab分析,调试AD9361信号通路
- mysql数据库简单指令_Mysql数据库一些简单命令
- 耶鲁大学校长2018迎新演讲全文
- 【文献阅读笔记】(2):使用IMPUTES2和minimac软件完成群体特异性的基因型填充(Imputation)
- nc系统java不能用_用友NC管理系统使用过程中常见问题和解决方法!
- The7强大多功能模板Var9.16.0+基于WordPress
- iOS: 设置背景颜色为渐变
- hive SQL 过滤不含数字的字段
热门文章
- mac下java 开发环境搭建
- qregexp限制数字范围_计算一列数字的平均值
- python空字典添加元素_python遍历删除字典里值为空的元素报错
- Python游戏开发pygame模块,Python实现球球碰撞小游戏
- Python面对对象编程——对象、类详解及实例
- mysql商品管理系统总结_Mysql管理总结
- cubemx pwm dma_红米K30S至尊纪念版翻车?被曝虽是LDC屏,却是PWM调光
- java 条件匹配_java语言实现满足多条件匹配简单过滤输出问题
- js layui 模板属性 添加_layui.laytpl--模板引擎文档
- ajax 切换列表,javascript实现列表切换效果