一、字符串

 #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)、字符串(一)的更多相关文章

  1. GO语言的进阶之路-Golang字符串处理以及文件操作

    GO语言的进阶之路-Golang字符串处理以及文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们都知道Golang是一门强类型的语言,相比Python在处理一些并发问题也 ...

  2. [c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset

    一.memset #include<stdio.h> #include<stdlib.h> #include<memory.h> void *mymemset(vo ...

  3. [c/c++] programming之路(24)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项

    1.将字符串插入到某位置(原字符串“hello yincheng hello cpp hello linux”,查找cpp,找到后在cpp的后面插入字符串“hello c”) 需要用到strstr字符 ...

  4. [c/c++] programming之路(23)、字符串(四)——strncat,atoi,strcmp,strlen等,以及常用内存函数

    一.strncat及自行封装实现 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #i ...

  5. [c/c++] programming之路(22)、字符串(三)——字符串封装

    项目结构 头文件.h #include<stdio.h> #include<stdlib.h> #include<string.h> //字符串封装,需要库函数 / ...

  6. [c/c++] programming之路(21)、字符串(二)

    一.for /l %i in (1,1,5) do calc 等命令行参数 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #inclu ...

  7. 我的Python自学之路-003 字符串的知识

    '''字符串是以引号或者单引号括起来的任意文本,例如"123","asdfjk",'adfa'引号或者单引号,只是一种表示方法,并不是字符串的一部分如果字符串本 ...

  8. Python之路 day2 字符串/元组/列表/字典互转

    #-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ' ...

  9. Python之路 day2 字符串函数

    #Author:ersa name = "ersa" #首字母大写capitalize() print(name.capitalize()) name = "my nam ...

随机推荐

  1. poj3278_kuagnbin带你飞专题一

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88247   Accepted: 27640 ...

  2. java的方法重载

    1丶java的方法重载特性 满足以下条件的两个或多个方法构成“重载”关系:(1)方法名相同 (2)参数类型不同,参数个数不同或者参数类型的顺序不同 像System.out.println一样,就是重载 ...

  3. 一次完整的从webshell到域控的探索之路

    前言 内网渗透测试资料基本上都是很多大牛的文章告诉我们思路如何,但是对于我等小菜一直是云里雾里. 于是使用什么样的工具才内网才能畅通无阻,成了大家一直以来的渴求. 今天小菜我本着所有师傅们无私分享的精 ...

  4. vue里的样式添加之类名改动 和style改动

    类名下有不同样式,通过增加或者减少类名,来增加或减少样式. v-bind:class = {类名:变量,类名:变量...}  变量为布尔值,如果是true则类名添加,反正类名不添加到该元素身上 v-b ...

  5. 教你如何用笔记本设置超快WIFI

    以win7为例 1.在主菜单运行框输入  cmd------->以管理员的身份运行 2.命令提示符中输入:netsh wlan set hostednetwork mode=allow ssid ...

  6. Java 将word转为pdf jacob方式

    package com.doctopdf; import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jac ...

  7. 实战一个职业技术学校。 by:hack某某

    这是我们的目标,某一技术学院,这是一个注入点 上sqlmap 跑出了管理账号密码 扫后台 没有找到,注入就相当鸡肋了 换换其他思路 dba权限,想到了直接写入 找找路径之类的 找到了,运气相当的好 直 ...

  8. 悬线法 || BZOJ3039: 玉蟾宫 || Luogu P4147 玉蟾宫

    题面: P4147 玉蟾宫 题解:过于板子举报了 #include<cstdio> #include<cstring> #include<iostream> #de ...

  9. 使用Xshell调用linux的图形界面!

    环境说明: OS: centos 6.5 64位,最小化安装. Xmanager: 17.0.0.714 1.设置Xshell 2.将操作系统安装如下包 yum  install xclock xte ...

  10. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...