一、字符串

 #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. Ubuntu系统安装nginx

    1.首先查看linux系统 cat /proc/version Linux version 4.9.59-v7+ (dc4@dc4-XPS13-9333) (gcc version 4.9.3 (cr ...

  2. js 画布与图片的相互转化(canvas与img)

    使用js将图片拷贝进画布 //将图片对象转化为画布,返回画布function ImageToCanvas(image) { var canvas = document.createElement(&q ...

  3. vue3版本到vue2版本的桥接工具

    vue2的命令可以正常使用.

  4. python __all__

    它不仅在第一时间展现了模块的内容大纲,而且也更清晰的提供了外部访问接口. 若__all__的list中未定义,即便有实现也会找不到.

  5. elk-filebeat-(效果图示)(四)

    一.vim filebeat-6.3.2-linux-x86_64/filebeat.yml - type: log # Change to true to enable this input con ...

  6. Linux 下安装mysql 5.7

    Linux 下安装mysql 5.7 本人首次安装时按照菜鸟教程的步骤一步一步来的,结果意外的是 装成5.6了,而且各种无厘头的问题,例如无法启动... 本文参照 大佬:‘这个名字想了很久~’ 的&l ...

  7. java应用,直接请求没问题,通过nginx跳转状态吗400

    今天配置金融的测试环境,直接调用java应用返回状态200,通通过nginx跳转,会返回400,真是一头雾水..... 参考文档: https://www.cnblogs.com/yanghj010/ ...

  8. 2018-2019-2 20175320实验一《Java开发环境的熟悉》实验报告

    2018-2019-2 20175320实验一<Java开发环境的熟悉>实验报告 一.实验步骤及内容 (一)带包程序的编译运行 1.使用mkdir命令创建如图所示目录 2.进入exp1下的 ...

  9. Luogu 1068 - 分数线划定 - [快速排序]

    题目链接:https://www.luogu.org/problemnew/show/P1068 题目描述世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对所有报名的选手 ...

  10. 用java代码解决excel打开csv文件乱码问题

      Java 读取csv文件后,再保存到磁盘上,然后直接用Excel打开,你会发现里面都是乱码. 贴上代码: public class Test { public static void main(S ...