[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 ...
随机推荐
- 【CF613D】Kingdom and its Cities 虚树+树形DP
[CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...
- web.py框架之i18n支持
问题: 在web.py的模板文件中, 如何得到i18n的支持? Solution: 项目目录结构: proj/ |- code.py |- i18n/ |- messages.po |- en_US/ ...
- Codeforces Round #531 (Div. 3)
A:瞎猜. #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); i ...
- gym102007 E
我计划预习五个小时离散,然后hmc补了这道他自认为非常的裸并且很傻逼自己可以一眼秒的简单题,然后给我讲了讲,然后我失去了一整晚的生命迹象. 首先我们可以发现一个神奇的现象,啊,先排个序,然后我们会发现 ...
- 写jquery插件(转载)
如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...
- 两矩阵各向量余弦相似度计算操作向量化.md
余弦相似度计算: \cos(\bf{v_1}, \bf{v_2}) = \frac{\left( v_1 \times v_2 \right)}{||v_1|| * ||v_2|| } \cos(\b ...
- devmapper: Thin Pool has 154464 free data blocks which is less than minimum required 163840 free dat
清理exited进程: docker rm $(docker ps -q -f status=exited) 清理dangling volumes: docker volume rm $(docker ...
- 【MySQL】锁——查看当前数据库锁请求的三种方法 20
MySQL提供了查看当前数据库锁请求的三种方法:1. show full processlist命令 观察state和info列 2. show engine innodb status\G ...
- Gym 101873G - Water Testing - [皮克定理]
题目链接:http://codeforces.com/gym/101873/problem/G 题意: 在点阵上,给出 $N$ 个点的坐标(全部都是在格点上),将它们按顺序连接可以构成一个多边形,求该 ...
- servlet转发重定向
1.request.getRequestDispacther("/test.jsp").forword(request,response); 转发 浏览器URL是一个地 ...