[c/c++] programming之路(20)、字符串(一)
一、字符串
#include<stdio.h>
#include<stdlib.h> void main(){
char str[]="notepad";
printf("%x\n",str);
printf("%c,%d\n",'\0','\0');
printf("%c,%d\n",,);
printf("%c,%d\n",'','');
system("pause");
}
#include<stdio.h>
#include<stdlib.h> void main(){
char str[]={'c','a','l','c'};
printf("%s\n",str);
system("pause");
}
字符串之后没有结束符'\0',直到遇到为止
二、指针
#include<stdio.h>
#include<stdlib.h> void main(){
char *p= "tasklist";
printf("%d,%d\t",sizeof(p),sizeof("tasklist"));
printf("%x\t",&p);
system(p);//本质上就是字符串的首地址
system("pause");
}
#include<stdio.h>
#include<stdlib.h> void main(){
char *p= "tasklist";
p = p + ;//只打印list
while (*p)//*p==0跳出循环,*p=='\0'
{
printf("%c,%x\n", *p,p);
p++;
} system("pause");
}
三、字符串数组
#include<stdio.h>
#include<stdlib.h> void main() {
//字符串数组
char str[][] = { { "notepad" },
{ "calc" },
{ "tasklist" },
{ "ipconfig" } };
char(*p)[] = str; //指向二维数组的指针,
for (int i = ; i < ; i++)//循环四次
{
system(p[i]); //字符串元素首地址
} system("pause");
}
#include<stdio.h>
#include<stdlib.h>
//字符串修改字符
void main() {
char str[] = "taskoist";
char *p = str;
p = p + ;
*p = 'l';
system(str);
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
//字符串修改字符
void main() {
char *p= "taskoist";//"taskoist"常量不可修改
char *ps = p;
ps += ;
//*ps = 'l';//运行时出错
system(p);
system("pause");
}
#include<stdio.h>
#include<stdlib.h> void main() {
char *p;
scanf("%s",p);//使用了未初始化的局部变量“p”
printf("%s",p);
system(p); system("pause");
}
解决方法一
char str[20];
char *p=str;
解决方法二
char *p=(char *)malloc(sizeof(char)*20);//指针必须指向一片内存
#include<stdio.h>
#include<stdlib.h> void main() {
char str[];
char str1[];
char str2[];
//gets(str1);//获取字符串初始化
gets_s(str1);//VS2015使用的是新C标准,也就是C11,而VC6.0用的是老标准。 在新标准中,应该是用gets_s代替gets
scanf("%s", str2); //获取字符串初始化
printf("%s %s", str1, str2);
sprintf(str, "%s %s", str1, str2); //字符相加
fprintf(stdout,"hello world"); system("pause");
}
四、求字符串长度
#include<stdio.h>
#include<stdlib.h> int getLen(const char *p) {//传入的字符串不允许被随意修改
int i = ;
if (p == NULL) return -;
else
{
while (*p) {
i++;
p++;
}
}
return i;
} void main() {
char str[] = "calc";
printf("%d\n",getLen(str));
system("pause");
}
五、获取CMD输出
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h> void execmd(char *in,char *out) {
FILE *pipe = _popen(in,"r");//read 读取
if (!pipe) return ; //判断管道是否为空
char buffer[] = {};
while (!feof(pipe)) { //判断文件是否结束
if (fgets(buffer, , pipe))//获取每一行的数据
{
strcat(out, buffer);//连接字符串
}
}
_pclose(pipe);//关闭管道
//return 1;
} void main() {
//注意:字符串需要初始化
char CMDin[] = { };//输入的指令
char CMDout[] = {};//输出的语句
scanf("%s",CMDin);//扫描输入
execmd(CMDin,CMDout);//获取结果
printf("打印的输出:%s",CMDout);//打印结果
printf("%x",CMDout);
//system(CMDin); system("pause");
}
六、字符串拼接
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//字符串处理函数 int getlen(char *str) {
int num = ;
while (*str) {//取出内容,0就是\0字符
num++;//计数一次
str++;//指针移动一次
}
return num;
} void mystrcat(char *all, char *add) {
int all_len = getlen(all);
int add_len = getlen(add);
char *pall = all;
char *padd = add;
pall += all_len;//指针移动到/0
while (*padd) {
*pall = *padd;
padd++;
pall++;
}
*(pall + ) = '\0';
} void main() {
char str[]="tracert";//遍历路由
char web[];
scanf("%s",web); //方法一
/*char cmd[100];
sprintf(cmd, "%s %s", str, web);
system(cmd);*/ //方法二
/*strcat(str, " ");
strcat(str,web);
system(str);*/ //方法三
mystrcat(str, " ");
mystrcat(str, web);
system(str);
}
七、字符串检索
#include<stdio.h>
#include<stdlib.h>
#include<string.h> int getlen(char *str) {
int num = ;
while (*str) {//取出内容,0就是\0字符
num++;//计数一次
str++;//指针移动一次
}
return num;
} char * findstr(char *all, char *str) {
char *p = NULL;
int all_len = getlen(all);
int str_len = getlen(str);
for (int i = ; i < all_len-str_len; i++)
{
int flag = ;//假定字符串相等
for (int j = ; j < str_len; j++)
{
if (all[i + j] != str[j]) {//判定字符是否相等
flag = ;
break;
}
}
if (flag == ) //如果为1,就是相等
{
p = &all[i];
break;
}
}
return p;
} void main() {
char all[] = "i love china i love cpp i love c";
//char str[30] = "i love cp";
char str[] = "i love cp0";
//char *p = strstr(all, str);
char *p = findstr(all, str);
if (p == NULL) printf("can not find!");
else
{
printf("find!\n");
printf("*p=%c\n",*p);//字符串检索的位置
}
system("pause");
}
八、字符串查找(指针查找)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h> int execmd(char *in, char *out)
{
char buffer[] = { };
FILE *pipe = _popen(in, "r");//读取
if (!pipe) //管道创建为空,返回0
{
return ;
}
while (!feof(pipe)) //判断文件是否结束
{
if (fgets(buffer, , pipe)) //获取每一行的数据
{
strcat(out,buffer);//连接字符串
}
}
_pclose(pipe);//关闭管道
return ;
} void main()
{
char CMDin[] = "tasklist";//查看所有的进程
char CMDout[] = { }; //输出的语句
execmd(CMDin, CMDout); //获取结果
char *p = strstr(CMDout, "QQ.exe");
if (p == NULL)
{
printf("QQ不存在");
}
else
{
printf("QQ 存在");
printf("*p=%c", *p);
}
getchar();
}
[c/c++] programming之路(20)、字符串(一)的更多相关文章
- GO语言的进阶之路-Golang字符串处理以及文件操作
GO语言的进阶之路-Golang字符串处理以及文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们都知道Golang是一门强类型的语言,相比Python在处理一些并发问题也 ...
- [c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset
一.memset #include<stdio.h> #include<stdlib.h> #include<memory.h> void *mymemset(vo ...
- [c/c++] programming之路(24)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项
1.将字符串插入到某位置(原字符串“hello yincheng hello cpp hello linux”,查找cpp,找到后在cpp的后面插入字符串“hello c”) 需要用到strstr字符 ...
- [c/c++] programming之路(23)、字符串(四)——strncat,atoi,strcmp,strlen等,以及常用内存函数
一.strncat及自行封装实现 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #i ...
- [c/c++] programming之路(22)、字符串(三)——字符串封装
项目结构 头文件.h #include<stdio.h> #include<stdlib.h> #include<string.h> //字符串封装,需要库函数 / ...
- [c/c++] programming之路(21)、字符串(二)
一.for /l %i in (1,1,5) do calc 等命令行参数 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #inclu ...
- 我的Python自学之路-003 字符串的知识
'''字符串是以引号或者单引号括起来的任意文本,例如"123","asdfjk",'adfa'引号或者单引号,只是一种表示方法,并不是字符串的一部分如果字符串本 ...
- Python之路 day2 字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...
- Python之路 day2 字符串函数
#Author:ersa name = "ersa" #首字母大写capitalize() print(name.capitalize()) name = "my nam ...
随机推荐
- .NET开发人员遇到Maven
由.NET转向Java开发,总是会带着.NET平台的一些概念和工具想着在对应的Java平台是否也有着相同的解决方案.第一次用Maven随手打开pom.xml,看着里面许多属性描述我的感觉就是这是一个M ...
- Java中的堆内存设置对线程创建数的影响以及-Xss参数的记录
Java的线程对象是存储在堆上的,所以,能够创建多少个线程,受到堆空间的大小限制,同时也受到每个线程的大小的限制,假如线程对象内部有一个非常大的数组字段,那就非常影响能够创建的线程的大小 我们的例子: ...
- VS2015编译rtklib2.4.2
准备工作 在VS2015下新建一个win32的dll项目(空项目) 把在github上下载的rtklib2.4.2里的src文件夹复制到刚刚建立的win32下 把src里的文件添加到项目里,其中头文件 ...
- java开学考试有感以及源码
一.感想 Java开学测试有感 九月二十号,王老师给我们上的第一节java课,测试. 说实话,不能说是十分有自信,但还好,直到看见了开学测试的题目,之前因为已经做过了王老师发的16级的题目,所以当时还 ...
- 前端自动化构建工具webpack (二)之css和插件加载总结
1. webpack只识别js文件,其他文件都需要转换成js文件.所有文件都是模块; 2. css解析 css需要css-loader --->style-loader ----- ...
- docker 打卡
create 2019/01/01 mod 2019/02/02 安装没得技术含量,看过菜鸟教程和纯洁写的博客,感觉so easy 命令: yum install docker 启动 设置开机启动 s ...
- java开发mis系统所需技术及其作用
MIS(管理信息系统--Management Information System)系统 ,是一个由人.计算机及其他外围设备等组成的能进行信息的收集.传递.存贮.加工.维护和使用的系统. 是一门新兴的 ...
- Win10系统jdk环境变量配置方法
http://www.w10zj.com/Win10xy/Win10yh_5620.html
- 公众号获取unionid
然后在微信客户端输入unionid接口的地址(比如发给文件传输助手www.XXX.COM/unionid.php),随便给别人发过去,在点击该链接,就能看到打印的accessToken,openid, ...
- 八、UIViewController们之间的协作——Segue
概述 正所谓“一生二,二生三,三生万物”,1个UIViewController没什么花样,多个UIViewController相互协作就有了各式各样丰富多彩的APP.但是UIViewControlle ...