C语言字符串操作函数实现
1、字符串反转 – strRev
void strRev(char *str)
{
assert(NULL != str);
int length=strlen(str);
char *end=str+length-1; while(end > str)
{
*str=(*str)^(*end);
*end=(*str)^(*end);
*str=(*str)^(*end); end--;
str++;
}
}
2、字符串复制 – strcpy
char *strcpy(char *strDest, const char *strStr)
{
assert((NULL != strDest) && (NULL != strStr)); char *Dest=strDest; while((*Dest++)=(*strStr++))
{} return strDest;
}
3、字符串拼接 –strcat
char *strcat(char *strDest, const char *strStr)
{
assert((NULL != strDest) && (NULL != strStr)); int length=strlen(strDest); char *Dest=strDest+length; while((*Dest++)=(*strStr++))
{} return strDest;
}
4、字符串比较 –strcmp
int strcmp(const char *strDest, const char *strStr)
{
assert((NULL != strDest) && (NULL != strStr)); while(0==(*strDest - *strStr) && *strDest )
{
strDest++;
strStr++;
} if(*strDest > *strStr)
return 1;
else if(*strDest < *strStr)
return -1;
else
return 0;
}
5、字符串长度 –strlen
int strlen(const char *strStr)
{
assert(NULL != strStr); int length = 0; while('\0' != *strStr)
{
length++;
strStr++;
} return length;
}
6、字符串转数字 –atoi
int atoi(const char *strStr)
{
assert(NULL != strStr); int minus = 0;
int begin = 0;
int sum = 0; while('\0' !=*strStr)
{
if(0==begin && (isdigit(*strStr) || '+'==*strStr || '-'==*strStr))
{
begin = 1; if('-'==*strStr)
{
minus = 1;
strStr++;
continue;
}
}
else if(!isdigit(*strStr))
{
printf("format is wrong !\n");
exit(1);
} if(1==begin)
{
sum = sum*10 + (*strStr-'0');
} strStr++;
} return minus ? (-sum):(sum);
}
7、数字转字符串 –atoi
char *itoa(int num)
{
int temp,i,j;
char array[10];
static char strDest[10]; for(temp = num,i=0; i<10 && temp ;i++)
{
array[i]=temp%10+'0';
temp/=10;
} for(i=i-1,j=0 ; i>=0 && j<10; i--,j++)
{
strDest[j]=array[i];
}
strDest[j]='\0'; return strDest;
}
8、计算字符串中元音字符的个数
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<ctype.h> int isVowel(char letter)
{
switch(toupper(letter))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
return 1;
default:
return 0;
}
} int countVowel(const char * strStr)
{
assert(NULL != strStr); int count=0; while('\0' !=*strStr++)
{
if(isVowel(*strStr))
count++;
} return count;
} int main()
{
char a[10]="hwlulr"; printf("%d\n",countVowel(a)); return 0;
}
9、判断一个字符串是否是回文
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<ctype.h> int isEqual(char a,char b)
{
if(a==b)
return 1; return 0;
} int isPalindrome(const char * strStr)
{
assert(NULL != strStr); int length=strlen(strStr);
int i,j; for(i=0,j=length-1; i<j ; i++,j--)
{
/*跳过空格和符号*/
while(' '== *(strStr+i) || !isalpha(*(strStr+i)))
i++;
while(' '== *(strStr+j) || !isalpha(*(strStr+j)))
j--; if(0==isEqual(*(strStr+i),*(strStr+j)))
return 0;
} return 1;
} int main()
{
char a[10]="heo o, e h"; printf("%s\n",isPalindrome(a) ? "is Palindrome" : "is not Palindrome"); return 0;
}
C语言字符串操作函数实现的更多相关文章
- 转: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语言字符串操作函数整理
整理C语言字符串相关的函数,以程序方式验证,以注释方式做说明. #include<stdio.h> #include<string.h> #include<stdlib. ...
- C语言字符串操作函数
1.函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include < ...
- C语言字符串操作函数总结
转载来源:https://blog.csdn.net/qq_33757398/article/details/81212618 字符串相关操作头文件:string.h 1.strcpy函数 原型:st ...
- C语言-字符串操作函数
gets(char buffer[]) 从标准输入读取一行, 并去掉换行符, 在字符串末尾增加 '\0' 字符, 写入到缓冲区 成功则返回 buffer 的地址, 出错或者遇到文件结尾则返回空指针, ...
- c语言字符串操作大全
C语言字符串操作函数 函数名: strcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #incl ...
- C语言的常用字符串操作函数(一)
一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...
- C语言字符串操作总结大全(超详细)
本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat( ...
随机推荐
- win 关闭正在使用的端口
1.Windows平台 在windows命令行窗口下执行: 1.查看所有的端口占用情况 C:\>netstat -ano 2.查看指定端口的占用情况 C:\>netstat -aon|fi ...
- webpack(3)--Output
Output output配置如何输出最终想要的代码,output是一个object里面包含一系列配置. 1. filename output.filename配置输出文件的名称,为string类型, ...
- CSS个人笔记
1. CSS盒模型 1.1 控制元素尺寸属性 1.1.1 box-sizing: 改变元素应用的尺寸规则 当设置元素尺寸宽度为固定值时(eg: 100px), 其实是元素内容区域的宽度为100px, ...
- django-mysql表的增删改查
1.增加数据 调用该路由执行ModelsCaozuo的处理方法 第一种实例化类 class ModelsCaozuo(View): ''' 数据库增加数据 ''' def get(self, requ ...
- npm WARN react-native-maps@0.14.0 requires a peer of react@>=15.4.0 but none was installed
install the react-native here comes a questions :: npm WARN react-native@0.41.2 requires a pe ...
- 关于putty连接百度云linux服务器那些事
看有活动,30元半年的百度云服务器,就直接买了当练手的玩 买完之后,发现使用putty不能直接连接百度云的centos服务器, 用了putty和ssh都不能连接 试了好几次,都打算尝试用秘钥对的形式了 ...
- 解决maven工程 子工程中的一些配置读取进来的问题
方案:在父工程中手动配置一些节点 <build> <!-- 插件 --> <plugins> <plugin> <groupId>org.a ...
- HTML5 Canvas ( 图形变换矩阵 ) transform, setTransform
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 19.OGNL与ValueStack(VS)-OGNL入门
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 下面我们在com.asm.vo.User类中增加一个字段private Ad ...
- delphi XE7 判断手机返回键
Using the Android Device's Back Button To make your application handle when users press the Back but ...