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

需要用到strstr字符串检索,strcpy字符串拷贝,strcat字符串拼接

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h> void main() {
char allstr[] = "hello yincheng hello cpp hello linux";
char findstr[] = "cpp";
char insertstr[] = "hello c"; char *p = strstr(allstr, findstr);//查找字符串
if(p==NULL)
printf("空指针,意味着没有找到\n");
else
{
printf("找到%c,地址%p\n", *p, p);
char temp[];
strcpy(temp, p + );//从p+4开始拷贝
printf("%s\n", temp);
*(p + ) = '\0';
strcat(allstr, insertstr);
strcat(allstr, temp);
printf("%s\n",allstr);
}
system("pause");
}

2.字符串和整数转化

预备知识

void main() {
printf("%d,%c\n", , );//1,编号为1的字符
printf("%d,%c\n",'','');//字符‘1’的编号49,字符‘1’
printf("%d\n",''-);//
system("pause");
}

字符串转整数

#include<stdio.h>
#include<stdlib.h> int tonum(char *str) {
char *istr = str;//保留副本
int num = ,sum=;
while (*str)
{
if (*str<'' || *str>'')
return -;
str++;
num++;//计数,判断有多少位
}
//str已经到了末尾
printf("%d\n",num);
for (int i = ; i < num; i++)
{
//int wei = str[i] - 48;//这句会导致结果错误,因为在上面的while循环中,str的地址已经发生了变化
int wei = istr[i] - ;
for (int j = i+; j < num; j++)
{
wei *= ;
}
sum += wei;
}
return sum;
} void main() {
char str[] = "";
int num = tonum(str);
printf("%d\n", num);
system("pause");
}

tonum函数另解(更简单)

int tonum(char *str) {
char *istr = str;//保留副本
int num = ,sum=;
while (*str)
{
if (*str<'' || *str>'')
return -;
str++;
num++;//计数,判断有多少位
}
//str已经到了末尾,继续使用str会出现数据错误
printf("%d\n",num);
for (int i = ; i < num; i++)
{
sum *= ;
int wei = istr[i] - ;
sum += wei;
}
return sum;
}

整数和字符串互转

#include<stdio.h>
#include<stdlib.h> int tonum(char *str) {
char *istr = str;//保留副本
int num = ,sum=;
while (*str)
{
if (*str<'' || *str>'')
return -;
str++;
num++;//计数,判断有多少位
}
//str已经到了末尾,继续使用str会出现数据错误
printf("%d\n",num);
for (int i = ; i < num; i++)
{
sum *= ;
int wei = istr[i] - ;
sum += wei;
}
return sum;
} void tostr(int num,char *str) {
int wei = ;
for (int inum = num; inum; inum /= )
wei++;
printf("wei=%d\n", wei);
for (int i = wei - ; num; num /= , i--)
{
//printf("%d,%d\n", num%10,i);
str[i] = num % + ;
}
} void main() {
int num = ;
char str[] = { };//编号为0的字符
tostr(num,str);
printf("%s\n", str); system("pause");
}

3.删除字符

#include<stdio.h>
#include<stdlib.h>
#include<string.h> void main() {
char str[] = "hello yincheng,hello c,hello cpp";
char ch = 'c';//要删除的字符
char last[] = { };//创建一个空字符串 char *p = str;
int i = ;
while (*p)
{
if (*p != ch) {
last[i] = *p;
i++;
}
p++;
}
printf("%s\n", last); system("pause");
}

4.模拟银行密码验证

输入三次错误就锁定,防止暴力穷举

#include<stdio.h>
#include<stdlib.h>
#include<string.h> void main() {
char pass[] = "password";
for (int i = ; i < ; i++)
{
char input[];
gets_s(input);//VS2015采用c11新标准,使用gets_s而不是gets:输入字符串并初始化
if (strcmp(input, pass) == ) {
printf("密码正确\n");
break;
}
else
printf("输入错误,你还有%d次机会\n", - i);
if(i==)
printf("密码输入三次都失败,账户已被锁定\n");
}
system("pause");
}

5.字符串输入注意事项

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h> void main0() {
char str[];
//scanf会将空格,回车,换行,换页,制表符当做终止符停止数据输入
scanf("%s", str);
printf("%s\n",str); char str1[];
scanf("%s", str1);
printf("%s\n", str1); system("pause");
} void main() {
char str[];
gets_s(str);//接收空格和制表符,遇到换行结束
printf("%s\n", str); system("pause");
}

[c/c++] programming之路(24)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项的更多相关文章

  1. PHP字符串指定位置插入字符串

    1.substr_replace(string,replacement,start,length);需插入时设置length为0即可 string 必需.规定要检查的字符串. replacement ...

  2. 【PHP】在目标字符串指定位置插入字符串

    PHP如何在指定位置插入相关字符串,例子:123456789变为1_23_456789插入"_"到指定的位置! (可以用作换行或者其他处理) 插入示例,具体思路在代码中有注释: & ...

  3. MySQL从删库到跑路(六)——SQL插入、更新、删除操作

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.插入数据 1.为表的所有字段插入数据 使用基本的INSERT语句插入数据要求指定表名称和插入到新记录的值. IN ...

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

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

  5. Python之路【第五篇】:面向对象及相关

    Python之路[第五篇]:面向对象及相关   面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否 ...

  6. Python之路【第五篇】:面向对象和相关

    Python之路[第五篇]:面向对象及相关   面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否 ...

  7. MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数

    一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位置 5.LEFT/RIGHT  取左 ...

  8. R学习笔记 第五篇:字符串操作

    文本数据存储在字符向量中,字符向量的每个元素都是字符串,而非单独的字符.在R中,可以使用双引号,或单引号表示字符,函数nchar用于获得字符串中的字符数量: > s='read' > nc ...

  9. Python基础学习参考(五):字符串和编码

     一.字符串 前面已经介绍过字符串,通过单引号或者双引号表示的一种数据类型.下面就再来进一步的细说一下字符串.字符串是不可变的,当你定义好以后就不能改变它了,可以进一步的说,字符串是一种特殊的元组,元 ...

随机推荐

  1. 关于vmware虚拟机硬件里没有软盘驱动器,而操作系统里还有的解决方法

    问题描述:今天笔者使用VMware 11.1.0安装了一个windows 7的操作系统,安装完成后进入系统发现 在计算机里的[有可移动存储的设备]下有一个软盘驱动器(A:)如下图所示: 但回到虚拟里设 ...

  2. PHP实现URL长连接转短连接方法总结

    短链接,通俗来说,就是将长的URL 网址,通过程序计算等方式,转换为简短的网址字符串. 这样的话其好处为:1.内容需要:2.用户友好:3.便于管理. 实现短网址(short URL)系统比较流行的算法 ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. javascript面试题(2)

    可以参考一  https://www.cnblogs.com/DCL1314/p/7903114.html 1.什么是JavaScript?(这是基本题,对很多程序员来说也是送分题!) JavaScr ...

  5. 小Q系列故事——屌丝的逆袭

    小Q系列故事——屌丝的逆袭 Problem Description 毕业于普通本科的小Q一直自称是资深屌丝,不仅学校不知名,甚至他自己在这个普通学校也是默默无闻——直到临近毕业的时候,班里5朵金花中的 ...

  6. Ubuntu12.04下解决sudo apt-get update警告Duplicate sources.list entry

    sudo apt-get update,会提示如下警告: W: Duplicate sources.list entry http://archive.canonical.com/ubuntu/ pr ...

  7. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

  8. Visual Studio 2017使用Asp.Net Core构建Angular4应用程序

    文章转载请著名出处:http://www.cnblogs.com/smallprogram 你需要了解的名词 1. NodeJS,这是一个基于Chrome V8 JavaScript引擎构建的Java ...

  9. 002-红黑树【B-树】、二叉查找树

    一.引述-二叉查找树 红黑树(Red Black Tree) 一种特殊的二叉查找树.故先查看二叉查找树 二叉查找树特性:左字数上所有的节点的值都小于或等于他的根节点上的值 右子树上所有节点的值均大于或 ...

  10. java框架之SpringBoot(13)-检索及整合Elasticsearch

    ElasticSearch介绍 简介 我们的应用经常需要使用检索功能,开源的 Elasticsearch 是目前全文搜索引擎的首选.它可以快速的存储.搜索和分析海量数据.SpringBoot 通过整合 ...