[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 ...
随机推荐
- Linux下的搜索查找命令的详解(which)
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件 ...
- oracle客户端instantclient如何配置
下载下来的instantclient-basic-nt-11.2.0.4.0.zip文件解压缩D:\Program Files\instantclient_11_2(可以选择自己的目录) (1) 增加 ...
- Mysql order by与limit混用陷阱
在Mysql中我们常常用order by来进行排序,使用limit来进行分页,当需要先排序后分页时我们往往使用类似的写法select * from 表名 order by 排序字段 limt M,N. ...
- java生成excel,word文件
第一部分: 在网站开发中,用户可能需要打印word或者excel表,这种需求是非常多的. java语言生成excel表格和python的方式有点像,使用Apache POI的组件,一通全通.开发过程通 ...
- 初始化后,composer安装
在项目目录下输入composer install(保证要有composer.json的前提下)
- react使用BrowserRouter打包后,刷新页面出现404
文档 https://gkedge.gitbooks.io/react-router-in-the-real/content/apache.html nginx nginx.conf server { ...
- windows下eclipse实现操作虚拟机ubantu中的hdfs hbase
1.首先打开虚拟机,查看虚拟机的ip地址 2.修改C:\Windows\System32\drivers\etc下的主机名与ip的映射文件 3.配置Map/reduce 配置成功后可以查看hdfs文件 ...
- HTML调用PC摄像头【申明:来源于网络】
HTML调用PC摄像头[申明:来源于网络] ---- 地址:http://www.oschina.net/code/snippet_2440934_55195 <!DOCTYPE html> ...
- xilink 烧写flash
no 右键
- [Day14]Eclipse高级、类与接口作为参数返回值
l 不同修饰符的使用 类,最常使用public修饰 成员变量,最常使用private修饰 成员方法,最常使用public修饰 l 自定义数据类型的使用 类作为方法参数时,说明要向方 ...