C语言字符串的操作
C语言字符串操作函数
1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. 字符串求长 - strlen
5. 字符串连接 - strcat6. 字符串比较 - strcmp
7. 计算字符串中的元音字符个数
8. 判断一个字符串是否是回文
1. 写一个函数实现字符串反转
版本1 - while版
void strRev(char *s)
{
char temp, *end = s + strlen(s) - 1;
while( end > s)
{
temp = *s;
*s = *end;
*end = temp;
--end;
++s;
}
}
版本2 - for版
void strRev(char *s)
{
char temp;
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
temp = *s;
*s = *end;
*end = temp;
}
}
版本3 - 不使用第三方变量
void strRev(char *s)
{
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
*s ^= *end;
*end ^= *s;
*s ^= *end;
}
}
版本4 - 重构版本3
void strRev(char *s)
{
for(char *end = s + strlen(s) - 1; end > s ; --end, ++s)
{
*s ^= *end ^= *s ^= *end;
}
}
版本5 - 重构版本4
void strRev(char *s)
{
for(char *end = s + strlen(s) - 1; end > s ; *s++ ^= *end ^= *s ^= *end--);
}
版本6 - 递归版
void strRev(const char *s)
{
if(s[0] == '\0')
return;
else
strRev(&s[1]);
printf("%c",s[0]);
}
2. 实现库函数strcpy的功能
strcpy函数位于头文件<string.h>中
版本1
strcpy(char * dest, const char * src)
{
char *p=dest;
while(*dest++ = *src++)
;
dest=p;
}
版本2
char * __cdecl strcpy(char * dst, const char * src)
{
char *p = dst;
while( *p ++ = *src ++ )
;
return dst;
}
版本3
strcpy(char * dest, const char * src)
{
int i=0;
for(; *(src+i)!='\0'; i++)
*(dest+i) = *(src+i);
*(dest+i) = '\0';
}
3. 实现库函数atoi的功能
atoi函数位于头文件<stdlib.h>中
版本1 - 附说明
int power(int base, int exp)
{
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 __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' )
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版
size_t __cdecl strlen(const char * s)
{
int i = 0;
while( *s )
{
i++;
s++;
}
return i;
}
版本2 - for版
size_t __cdecl strlen(const char * s)
{
for(int i = 0; *s; i++, s++)
;
return i;
}
版本3 - 无变量版
size_t __cdecl strlen(const char * s)
{
if(*s == '\0')
return 0;
else
return (strlen(++s) + 1);
}
版本4 - 重构版本3
size_t __cdecl strlen(const char * s)
{
return *s ? (strlen(++s) + 1) : 0;
}
5. 实现库函数strcat的功能
strcat函数位于头文件<string.h>中
版本1 - while版
char * __cdecl strcat(char * dst, const char * src)
{
char *p = dst;
while( *p )
p++;
while( *p ++ = *src ++ )
;
return dst;
}
6. 实现库函数strcmp的功能
strcmp函数位于头文件<string.h>中
版本1 - 错误的strcmp
int strcmp(const char * a, const char * b)
{
for(; *a !='\0' && *b !='\0'; a++, b++)
if( *a > *b)
return 1;
else if ( *a==*b)
return 0;
else
return -1;
}
版本2
int __cdecl strcmp (const char * src, const char * dst)
{
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. 计算字符串中元音字符的个数
#include <stdio.h> 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?
Don't nod
Dogma: I am God
Never odd or even
Too bad – I hid a boot
Rats live on no evil star
No trace; not one carton
Was it Eliot's toilet I saw?
Murder for a jar of red rum
May a moody baby doom a yam?
Go hang a salami; I'm a lasagna hog!
Satan, oscillate my metallic sonatas!
A Toyota! Race fast... safe car: a Toyota
Straw? No, too stupid a fad; I put soot on warts
Are we not drawn onward, we few, drawn onward to new era?
Doc Note: I dissent. A fast never prevents a fatness. I diet on cod
No, it never propagates if I set a gap or prevention
Anne, I vote more cars race Rome to Vienna
Sums are not set as a test on Erasmus
Kay, a red nude, peeped under a yak
Some men interpret nine memos
Campus Motto: Bottoms up, Mac
Go deliver a dare, vile dog!
Madam, in Eden I'm Adam
Oozy rat in a sanitary zoo
Ah, Satan sees Natasha
Lisa Bonet ate no basil
Do geese see God?
God saw I was dog
Dennis sinned
世界之最:世界上最长的回文包含了17,259个单词
C语言字符串的操作的更多相关文章
- Go 语言字符串常见操作
@ 目录 1. 字节数组 2. 头尾处理 3. 位置索引 4. 替换 5. 统计次数 6. 重复 7. 大小写 8. 去除字符 9. 字符串切片处理 10. 数值处理 1. 字节数组 字节与字符的区别 ...
- C语言 字符串操作 笔记
/* C语言字符串的操作笔记 使用代码和注释结合方式记录 */ # include <stdio.h> # include <string.h> int main(void) ...
- C语言字符串操作总结大全(超详细)
本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat( ...
- C语言字符串操作常用库函数
C语言字符串操作常用库函数 *********************************************************************************** 函数 ...
- c语言字符串操作大全
C语言字符串操作函数 函数名: strcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #incl ...
- 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...
- C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文
原文:http://www.cnblogs.com/JCSU/articles/1305401.html C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. ...
- C语言字符串操作函数总结
转载来源:https://blog.csdn.net/qq_33757398/article/details/81212618 字符串相关操作头文件:string.h 1.strcpy函数 原型:st ...
- 零基础学习C语言字符串操作总结大全
本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, ...
随机推荐
- cookie用法小结 cookie.setPath 跨域共享
1. JSP中Cookie的读写 Cookie的本质是一个键值对,当浏览器访问web服务器的时候写入在客户端机器上,里面记录一些信息.Cookie还有一些附加信息,比如域名.有效时间.注释等等. 下面 ...
- javascript总结31 :DOM概述
1 JavaScript 三个组成部分 核心(ECMAScript)欧洲计算机制造商协会 描述了JS的语法和基本对象. 文档对象模型(DOM) 处理网页内容的方法和接口 浏览器对象模型(BOM) 与浏 ...
- up6-自定义文件存储路径
在up6.2中有两种保存模式,一种是md5一种是uuid. md5由PathMd5Builder生成存储路径.md5主要提供给文件使用,可在服务器端保存唯一的文件,有效避免重复文件. uuid由Pat ...
- 利用APT实现Android编译时注解
摘要: 一.APT概述 我们在前面的java注解详解一文中已经讲过,可以在运行时利用反射机制运行处理注解.其实,我们还可以在编译时处理注解,这就是不得不说官方为我们提供的注解处理工具APT (Anno ...
- LightOJ 1098(均值不等式,整除分块玄学优化)
We all know that any integer number n is divisible by 1 and n. That is why these two numbers are not ...
- html中怎么样让div并排显示
html中的div默认是流式显示,每个div会占用一整行 <html> <head> <meta http-equiv="Content-Type" ...
- Vc6.0 编译发生致命链接错误 :不能打开exe的文件
错误: fatal error LNK1104: cannot open file "Debug/CeshiToolBar1.exe" 解决方法:打开任务管理器,找到对应的exe应 ...
- Delphi xe5如何使用Bluestacks模拟器(用真机或者用猩猩,夜神模拟器,自带的不好用)
首先,关于这个模拟器问题比较纠结,这是一个关于adb的问题. Delphi XE5会自动识别模拟器和真机,但是你必须先打开模拟器在打开Delphi IDE(Delphi开发环境),否则还得麻烦一会儿. ...
- duilib入门简明教程 -- 界面设计器 DuiDesigner (10)
上一个教程讲解了怎么布局最大化.最小化.关闭按钮,但是如果手动去计算这三个按钮的位置和大小的话,非常的不直观,也很不方便. 所以这一章准备介绍duilib的UI设计器,由于这个设计器很不 ...
- C#在线运行
初步完成c#代码的在线编辑. 首先,传回前端的c#在线代码,进行预编译,用CSharpCodeProvider这个方法.设置编译版本3.5 设置编译参数GenerateInMemory:是 ...