C++字符串操作二
#include <iostream>
#include <assert.h>
using namespace std;
//模拟实现strcmp函数。
bool my_strcmp(const char *str1,const char *str2)
{
assert(str1!=NULL && str2!=NULL);
const char *p = str1;
const char *q = str2;
while (*p != '\0' && *q != '\0' && *p == *q)
{
p++;
q++;
}
return (*p - *q)!=0;
}
int main()
{
cout << my_strcmp("liwu", "liu") << endl;
return 0;
}
#include <iostream>
#include <assert.h>
using namespace std;
//库函数memcopy的实现。
char *my_memcopy(char *dist, const char *src, int len)
{
assert(dist != NULL && src != NULL);
char *p = dist;
int n = len>(strlen(src)+1) ?
strlen(src)+1 : len;
while (n-->0)
{
*dist++ = *src++;
}
return p;
}
int main()
{
char s1[20] = "1234";
my_memcopy(s1,"liuhuiyan",2);
cout << s1 << endl;
}
#include <iostream>
#include <assert.h>
using namespace std;
char* my_memove(char *dist,const char* src,int len)
{
assert(dist!=NULL && src!=NULL);
char *p = dist;
const char *q = src;
int n = len>(strlen(src) + 1) ? strlen(src) + 1 : len;
if (p <= q || q+n<=p)
{
while (n-- > 0)
{
*p++ = *q++;
}
}
else
{
p += n-1;
q += n-1;
while (n-- > 0)
{
*p-- = *q--;
}
}
return dist;
}
int main()
{
char s1[] = "liu hui yan";
char s2[] = "123 456";
cout << my_memove(s1, s2, 6) << endl;
return 0;
}
#include <iostream>
using namespace std;
//一个字符串“student a am i”,现要求将这个字符串改动为“i am a student”。
void exchange(char *p1,char *p2)
{
char temp;
while (p1 < p2)
{
temp = *p1;
*p1 = *p2;
*p2 = temp;
p1++;
p2--;
}
}
char* my_exchange(char *src)
{
char *p = src;
char *q = src;
while (*p != '\0')
{
while (*p != ' ' && *p!='\0')
{
p++;
}
exchange(q,p-1);
if (*p == '\0')break;
p++;
q = p;
}
exchange(src,p-1);
return src;
}
int main()
{
char s[] = "student a am i";
cout << my_exchange(s) << endl;
}
//字符串打印数字。
#include <iostream>
#include <assert.h>
using namespace std;
int my_int(const char *str)
{
assert(str!=NULL);
int count = 0;
while (*str != '\0')
{
count = (count * 10 + *str - '0');
str++;
}
return count;
}
int main()
{
char s[] = "12345";
cout << my_int(s) << endl;
return 0;
}
#include <iostream>
using namespace std;
char str[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
//输入数字1234,打印对应的字符串。
void Printf(int x)
{
if (x==0)return;
else
{
Printf(x / 10);
cout << str[x % 10] << endl;
}
}
int main()
{
Printf(1234);
return 0;
}
C++字符串操作二的更多相关文章
- python 字符串操作二 内建函数
一.查看字符串的内建函数 >>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__' ...
- [Redis-CentOS7]Redis字符串操作(二)
登录Redis # redis-cli 127.0.0.1:6379> 添加字符串 EX 超期时间60s 127.0.0.1:6379> set username 'leoshi' OK ...
- 【二】php 字符串操作及三大流程控制
字符串操作: trim:去除字符串开始位置和结束位置的空格 ltrim:去除开始处的空格 rtrim:去除结束处的空格 strtoupper:将字符串转换为大写 strtolower:将字符串转换为小 ...
- python基础(二)-- 列表、字典、集合、字符串操作
4.列表: 基本操作: 索引 切片 追加 删除 长度 切片 循环 包含 import copy i=0 #persons=['dailaoban','xiekeng',['age',100,50],' ...
- java 字符串操作和日期操作
一.字符串操作 创建字符串 String s2 = new String("Hello World"); String s1 = "Hello World"; ...
- C语言字符串操作总结大全
1)字符串操作 strcpy(p, p1) 复制字符串 函数原型strncpy(p, p1, n) 复制指定长度字符串 函数原型strcat(p, p1) 附加字符串 函数原型strn ...
- c# 字符串操作
一.字符串操作 //字符串转数组 string mystring="this is a string" char[] mychars=mystring.ToCharArray(); ...
- C语言字符串操作总结大全(超详细)
本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat( ...
- linux shell 字符串操作
转:http://justcoding.iteye.com/blog/1963463 在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作 ...
随机推荐
- 利用hibernate与struts框架制作简单注册界面
一:配置hibernate 1.导包 hibernate包和jdbc连接mysql数据库的包 2.实用工具生成hibernate配置文件和映射文件 3.做好hibernateUtil生成session ...
- 刷题总结——做运动(NOIP模拟)
题目: 给定一个无向图,节点数n<=50000,m<=1000000,每条边有两个值t和c,边的长度为t*c···现在要求再t尽量小的情况下,求两节点st的最短距离 题解: 第一次做的时候 ...
- chromedriver对应的支持的Chrome版本(更新至Chrome64)
很多网友在配置chromedriver的时候会遇到很多麻烦,在网上找了很多资料觉得这个表格不错,就给大家分享出来,希望对大家配置chrome的时候有帮助: chromedriver版本 支持的Chro ...
- Flask获取post提交数据
完成示例 # flask 代码 from flask import Flask,request ... @app.route('/get_tasks',methods=["POST" ...
- 推荐个PMP的内容,广州有需要的朋友可以参考看看
慧翔天地PMP®培训机构简介 广州慧翔企业管理咨询有限公司注册于2012年8月14日,注册资金200万元人民币.实际上从2011年就已经开始从事PMP推广及教学工作(2010年曾代理智鼎东方华南市场, ...
- SharePoint 2013 App 开发—App开发概述
基于安全性的考虑,SharePoint App 不能像其它两种方式一样,直接使用安全性更高的服务端代码的API.Javascript 扮演极为重要的角色,在SharePoint App中与ShareP ...
- 快速比對 修改的檔案 使用 Beyond Compare Filters & git & sed
修改 code 後, 想使用 beyond compare 比對 修改前後的 code (有一包未修改的 code), 若 code 很大, 全部比完,需要花很多時間, Command 此時可以使用 ...
- PHP 几个常用的正则表达式
记录几个PHP中比较常用的正则. , $max_len = 20){ if (empty($username)) { return false; } $match = '/^(?![0-9]+$)[\ ...
- 无密码登录Linux
配置主机A无密码登录主机B 主机A:192.168.1.110 主机B:192.168.1.111 先确保所有主机的防火墙处于关闭状态. 在主机A上执行如下: 1. $cd ~/.ssh 2. $ss ...
- ARCGIS SDK For DotNet 路径
ARCGIS SDK For DotNet 路径 驱动器 C 中的卷是 WIN7 卷的序列号是 06AC-BD3E C:\Program Files (x86)\ArcGIS\DeveloperKit ...