C/C++ 字符串拷贝处理
C语言的字符串操作
strtok 实现字符串切割: 将字符串根据分隔符进行切割分片.
#include <stdio.h>
int main(int argc, char* argv[])
{
char str[] = "hello,lyshark,welcome";
char *ptr;
ptr = strtok(str, ",");
while (ptr != NULL)
{
printf("切割元素: %s\n", ptr);
ptr = strtok(NULL, ",");
}
system("pause");
return 0;
}
strlen 获取字符串长度:
#include <stdio.h>
int main(int argc, char* argv[])
{
char Array[] = "\0hello\nlyshark";
char Str[] = { 'h', 'e', 'l', 'l', 'o' };
int array_len = strlen(Array);
printf("字符串的有效长度:%d\n", array_len);
int str_len = strlen(Str);
printf("字符串数组有效长度: %d\n", str_len);
int index = 0;
while (Str[index] != '\0')
{
index++;
printf("Str数组元素: %c --> 计数: %d \n", Str[index], index);
}
system("pause");
return 0;
}
strcpy 字符串拷贝:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char Array[] = "hello lyshark";
char tmp[100];
// 学习strcpy函数的使用方式
if (strcpy(tmp, Array) == NULL)
printf("从Array拷贝到tmp失败\n");
else
printf("拷贝后打印: %s\n", tmp);
// 清空tmp数组的两种方式
for (unsigned int x = 0; x < strlen(tmp); x++)
tmp[x] = ' ';
memset(tmp, 0, sizeof(tmp));
// 学习strncpy函数的使用方式
if (strncpy(tmp, Array, 3) == NULL)
printf("从Array拷贝3个字符到tmp失败\n");
else
printf("拷贝后打印: %s\n", tmp);
system("pause");
return 0;
}
strcat字符串连接: 将由src指向的空终止字节串的副本追加到由dest指向的以空字节终止的字节串的末尾
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char str1[50] = "hello ";
char str2[50] = "lyshark!";
char * str = strcat(str1, str2);
printf("字符串连接: %s \n", str);
str = strcat(str1, " world");
printf("字符串连接: %s \n", str);
str = strncat(str1, str2, 3);
printf("字符串连接: %s \n", str);
system("pause");
return 0;
}
strcmp 字符串对比:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int Str_Cmp(const char * lhs, const char * rhs)
{
int ret = strcmp(lhs, rhs);
if (ret == 0)
return 1;
else
return 0;
}
int main(int argc, char* argv[])
{
char *str1 = "hello lyshark";
char *str2 = "hello lyshark";
int ret = Str_Cmp(str1, str2);
printf("字符串是否相等: %d \n", ret);
if (!strncmp(str1, str2, 3))
printf("两个字符串,前三位相等");
system("pause");
return 0;
}
strshr 字符串截取:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
const char str[] = "hello ! lyshark";
char *ret;
ret = strchr(str, '!');
printf("%s \n", ret);
system("pause");
return 0;
}
字符串逆序排列:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Swap_Str(char *Array)
{
int len = strlen(Array);
char *p1 = Array;
char *p2 = &Array[len - 1];
while (p1 < p2)
{
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++, p2--;
}
}
int main(int argc, char* argv[])
{
char str[20] = "hello lyshark";
Swap_Str(str);
for (int x = 0; x < strlen(str); x++)
printf("%c", str[x]);
system("pause");
return 0;
}
实现字符串拷贝:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 使用数组实现字符串拷贝
void CopyString(char *dest,const char *source)
{
int len = strlen(source);
for (int x = 0; x < len; x++)
{
dest[x] = source[x];
}
dest[len] = '\0';
}
// 使用指针的方式实现拷贝
void CopyStringPtr(char *dest, const char *source)
{
while (*source != '\0')
{
*dest = *source;
++dest, ++source;
}
*dest = '\0';
}
// 简易版字符串拷贝
void CopyStringPtrBase(char *dest, const char *source)
{
while (*dest++ = *source++);
}
int main(int argc, char* argv[])
{
char * str = "hello lyshark";
char buf[1024] = { 0 };
CopyStringPtrBase(buf, str);
printf("%s \n", buf);
system("pause");
return 0;
}
格式化字符串:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// 格式化填充输出
char buf[30] = { 0 };
sprintf(buf, "hello %s %s", "lyshark","you are good");
printf("格式化后: %s \n", buf);
// 拼接字符串
char *s1 = "hello";
char *s2 = "lyshark";
memset(buf, 0, 30);
sprintf(buf, "%s --> %s", s1, s2);
printf("格式化后: %s \n", buf);
// 数字装换位字符串
int number = 100;
memset(buf, 0, 30);
sprintf(buf, "%d", number);
printf("格式化后: %s \n", buf);
system("pause");
return 0;
}
动态存储字符串:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// 分配空间
char **p = malloc(sizeof(char *)* 5);
for (int x = 0; x < 5;++x)
{
p[x] = malloc(64);
memset(p[x], 0, 64);
sprintf(p[x], "Name %d", x + 1);
}
// 打印字符串
for (int x = 0; x < 5; x++)
printf("%s \n", p[x]);
// 释放空间
for (int x = 0; x < 5; x++)
{
if (p[x] != NULL)
free(p[x]);
}
system("pause");
return 0;
}
字符串拼接:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * StringSplicing(char *String1, char *String2)
{
char Buffer[1024];
int index = 0;
int len = strlen(String1);
while (String1[index] != '\0')
{
Buffer[index] = String1[index];
index++;
}
while (String2[index - len] != '\0')
{
Buffer[index] = String2[index - len];
index++;
}
Buffer[index] = '\0';
char *ret = (char*)calloc(1024, sizeof(char*));
if (ret)
strcpy(ret, Buffer);
return ret;
}
int main(int argc, char* argv[])
{
char *str1 = "hello ";
char *str2 = "lyshark ! \n";
char * new_str = StringSplicing(str1, str2);
printf("拼接好的字符串是: %s", new_str);
system("pause");
return 0;
}
实现strchr:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * MyStrchr(const char *String, char ch)
{
char *ptr = String;
while (*ptr != '\0')
{
if (*ptr == ch)
return ptr;
ptr++;
}
return NULL;
}
int main(int argc, char* argv[])
{
char Str[] = "hello lyshark";
char ch = 's';
char *ptr = MyStrchr(Str, ch);
printf("输出结果: %s \n", ptr);
system("pause");
return 0;
}
自己实现寻找字符串子串:
#include <stdio.h>
#include <stdlib.h>
// 查找子串第一次出现的位置
char *MyStrStr(const char* str, const char* substr)
{
const char *mystr = str;
const char *mysub = substr;
while (*mystr != '\0')
{
if (*mystr != *mysub)
{
++mystr;
continue;
}
char *tmp_mystr = mystr;
char *tmp_mysub = mysub;
while (tmp_mysub != '\0')
{
if (*tmp_mystr != *tmp_mysub)
{
++mystr;
break;
}
++tmp_mysub;
}
if (*tmp_mysub == '\0')
{
return mystr;
}
}
return NULL;
}
int main(int argc, char* argv[])
{
char *str = "abcdefg";
char *sub = "fg";
char * aaa = MyStrStr(str, sub);
printf("%s", aaa);
system("pause");
return 0;
}
删除字符串中连续字符
#include <stdio.h>
char del(char s[],int pos,int len) //自定义删除函数,这里采用覆盖方法
{
int i;
for (i=pos+len-1; s[i]!='\0'; i++,pos++)
s[pos-1]=s[i]; //用删除部分后的字符依次从删除部分开始覆盖
s[pos-1]='\0';
return s;
}
int main(int argc, char *argv[])
{
char str[50];
int position,length;
printf ("please input string:\n");
gets(str); //使用gets函数获得字符串
printf ("please input delete position:");
scanf("%d",&position);
printf ("please input delete length:");
scanf("%d",&length);
del(str,position,length);
printf ("the final string:%s\n",str);
return 0;
}
C++的字符串操作
在C语言中想要输出数据需要使用Printf来实现,但C++中引入了另一种输出方式,C++中形象的将此过程称为流,数据的输入输出是指由若干个字节组成的字节序列,这些序列从一个对象中传递到另一个对象,我们将此过程形象的表示为数据的流,数据流可以包括ASCII字符,二进制数据,图形图像数据,音频数据等,流都将可以操作.
字符串类的初始化:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string str("hello lyshark"); // 定义一个字符串
string str_1(str); // 构造函数,将 str中的内容全部复制到str_1
cout << str_1 << endl;
string str_2(str, 2, 5); // 构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str_2
cout << str_2 << endl;
string str_3(str.begin(), str.end()); // 复制字符串 str 的所有元素,并赋值给 str_3
cout << str_3 << endl;
char ch[] = "lyshark";
string str_4(ch, 3); // 将字符串ch的前5个元素赋值给str_4
cout << str_4 << endl;
string str_5(5, 'x'); // 将 5 个 'X' 组成的字符串 "XXXXX" 赋值给 str_5
cout << str_5 << endl;
system("pause");
return 0;
}
标准输出流: 首先我们演示标准的输入输出,其需要引入头文件<iostream>
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
char str[] = "lyshark";
int number = 0;
cout << "hello: " << str << endl;
cin >> number;
if (number == 0)
{
cerr << "Error msg" << endl; // 标准的错误流
clog << "Error log" << endl; // 标准的日志流
}
int x, y;
cin >> x >> y; // 一次可以接受两个参数
freopen("./test.log", "w", stdout); // 将标准输出重定向到文件
system("pause");
return 0;
}
格式化输出: 在程序中一般用cout和插入运算符“<<”实现输出,cout流在内存中有相应的缓冲区。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char* argv[])
{
cout << hex << 100 << endl; // 十六进制输出
cout << dec << 100 << endl; // 十进制输出
cout << oct << 100 << endl; // 八进制输出
cout << fixed << 10.053 << endl; // 单浮点数输出
cout << scientific << 10.053 << endl; // 科学计数法
cout << setw(10) << "hello" << setw(10) << "lyshark" << endl; // 默认两个单词之间空格
cout << setfill('-') << setw(10) << "hello" << endl; // 指定域宽,输出字符串,空白处以'-'填充
for (int x = 0; x < 3; x++)
{
cout << setw(10) << left << "hello" ; // 自动(left/right)对齐,不足补空格
}
cout << endl;
system("pause");
return 0;
}
单个字符输出: 流对象中,提供了专用于输出单个字符的成员函数put
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char* argv[])
{
char *str = "lyshark";
for (int x = 6; x >= 0; x--)
cout.put(*(str + x)); // 每次输出一个字符
cout.put('\n');
system("pause");
return 0;
}
标准输入流: 通过测试cin的真值,判断流对象是否处于正常状态.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char* argv[])
{
float grade;
while (cin >> grade)
{
if (grade >= 85)
cout << grade << " good" << endl;
}
system("pause");
return 0;
}
读取字符串: getline函数的作用是从输入流中读取一行字符
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char* argv[])
{
char str[20];
int x, y, z;
cin >> x >> y >> z;
cout << x << y << z;
cin.getline(str, 20); // 读入字符遇到\n结束读取
cout << str << endl;
cin.getline(str, 20, 'z'); // 读入字符遇到z字符才结束
cout << str << endl;
system("pause");
return 0;
}
C/C++ 字符串拷贝处理的更多相关文章
- C语言字符串拷贝
C语言字符串拷贝利用指针操作,要清楚知道指针的指向 代码如下: #include <stdio.h> #include <assert.h> #include <stri ...
- C++内存问题大集合(指针问题,以及字符串拷贝问题,确实挺危险的)
作者:rendao.org,版权声明,转载必须征得同意. 内存越界,变量被篡改 memset时长度参数超出了数组长度,但memset当时并不会报错,而是操作了不应该操作的内存,导致变量被无端篡改 还可 ...
- 字符串拷贝函数strcpy写法_转
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- Com组件的内存分配和释放,CredentialProvider SHStrDup 字符串拷贝问题
一.简单介绍 熟悉CredentialProvider的同学应该知道,他为一个Com组件,于是,在这里的内存分配(字符串拷贝)的一系列操作就要依照con的标准来. 二.Com组件的内存分配和释放 CO ...
- [转]delphi 有授权许可的字符串拷贝函数源码
一段看上去“貌不惊人”的Delphi插入汇编代码,却需要授权许可,但是与经典的同类型函数比较,确实“身手不凡”. 研究代码的目的在于借鉴,本文通过分析,并用C++重写代码进行比较,再次证明这段代码效率 ...
- C语言——常用标准输入输出函数 scanf(), printf(), gets(), puts(), getchar(), putchar(); 字符串拷贝函数 strcpy(), strncpy(), strchr(), strstr()函数用法特点
1 首先介绍几个常用到的转义符 (1) 换行符“\n”, ASCII值为10: (2) 回车符“\r”, ASCII值为13: (3) 水平制表符“\t”, ASCII值为 9 ...
- 实现字符串检索strstr函数、字符串长度strlen函数、字符串拷贝strcpy函数
#include <stdio.h> #include <stdlib.h> #include <string.h> /* _Check_return_ _Ret_ ...
- 编写实现字符串拷贝函数strcpy()完整版
有个题目编程实现字符串拷贝函数strcpy(),很多人往往很快就写出下面这个代码. void strcpy( char *strDest,char *strSrc ) { while(( *strDe ...
- Strlcpy和strlcat——一致的、安全的字符串拷贝和串接函数【转】
转自:http://blog.csdn.net/kailan818/article/details/6731772 英文原文: http://www.gratisoft.us/todd/papers/ ...
- 利用string 字符串拷贝
序言:对于laws的代码,完全从Matlab中转来.其中用到了字符串复制和对比的函数. C++要求: 输入字符串,根据字符串,来确定选择数组,用于下一过程 MatLab代码: (1).文件calLaw ...
随机推荐
- ABAP 内表与JSON转换
一.内表转JSON "-----------------------------@斌将军----------------------------- TYPES: BEGIN OF ty_na ...
- AcWing 第 1 场周赛补题记录(A~C)
比赛链接:Here AcWing 3577. 选择数字 排序,然后选取两个数组的最大值 void solve() { int n; cin >> n; vector<int>a ...
- 蓝桥杯历年省赛试题汇总 C/C++ A组
A组 省赛 B 组的题目可以在这里查看 → 刷题笔记: 蓝桥杯 题目提交网站:Here 2013 第四届 高斯日记 排它平方数 振兴中华 颠倒的价牌 前缀判断 逆波兰表达式 错误票据 买不到的数目 剪 ...
- Android 多语言动态更新方案探索
本文首发于 vivo互联网技术 微信公众号 链接: https://mp.weixin.qq.com/s/jG8rAjQ8QAOmViiQ33SuEg作者:陈龙 最近做的项目需要支持几十种语言,很多小 ...
- 3D编程模式:介绍设计原则
大家好~本文介绍6个设计原则的定义 系列文章详见: 3D编程模式:开篇 目录 单一职责原则(SRP) 依赖倒置原则(DIP) 接口隔离原则(ISP) 迪米特法则(LoD) 合成复用原则(CARP) 开 ...
- js判断undefined
if (item2.shifoushiyong === 1) { if( typeof(item2.koufen) == "undefined" ) { item2.koufen ...
- P1216-DP【橙】
在这道题中,我第一次用了memset,确实方便,不过需要注意的是只有全部赋值-1和0的时候才能使用它,否则他能干出吓死人的事.以及memset在cstring头文件里,在本地就算不include也能照 ...
- centos7 firewalld配置常用规则
限制ssh服务只允许某个ip ### 允许某个ip(调整前,务必添加定时任务`29 17 * * * systemctl stop firewalld`) firewall-cmd --permane ...
- Linux进阶命令-grep
Linux进阶命令----grep 目录 Linux进阶命令----grep grep 命令介绍 grep命令格式 常用选项 模式部分 匹配字符: 匹配次数: 位置锚定: grep 命令介绍 Linu ...
- 新建Maven工程没有src/main...目录?
0.必看:详细的Maven项目介绍 1.问题 我新建的Maven项目的pom.xml为空,且无法被识别,同时项目目录没有src/main等等 2.解决 这里设置的JDK版本不对,我选用了JDK19 但 ...