[c/c++] programming之路(24)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项
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)、字符串(五)——字符串插入,字符串转整数,删除字符,密码验证,注意事项的更多相关文章
- PHP字符串指定位置插入字符串
1.substr_replace(string,replacement,start,length);需插入时设置length为0即可 string 必需.规定要检查的字符串. replacement ...
- 【PHP】在目标字符串指定位置插入字符串
PHP如何在指定位置插入相关字符串,例子:123456789变为1_23_456789插入"_"到指定的位置! (可以用作换行或者其他处理) 插入示例,具体思路在代码中有注释: & ...
- MySQL从删库到跑路(六)——SQL插入、更新、删除操作
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.插入数据 1.为表的所有字段插入数据 使用基本的INSERT语句插入数据要求指定表名称和插入到新记录的值. IN ...
- [c/c++] programming之路(21)、字符串(二)
一.for /l %i in (1,1,5) do calc 等命令行参数 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #inclu ...
- Python之路【第五篇】:面向对象及相关
Python之路[第五篇]:面向对象及相关 面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否 ...
- Python之路【第五篇】:面向对象和相关
Python之路[第五篇]:面向对象及相关 面向对象基础 基础内容介绍详见一下两篇博文: 面向对象初级篇 面向对象进阶篇 其他相关 一.isinstance(obj, cls) 检查是否obj是否 ...
- MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数
一.常见字符串函数: 1.CHAR_LENGTH 获取长度(字符为单位) 2.FORMAT 格式化 3.INSERT 替换的方式插入 4.INSTR 获取位置 5.LEFT/RIGHT 取左 ...
- R学习笔记 第五篇:字符串操作
文本数据存储在字符向量中,字符向量的每个元素都是字符串,而非单独的字符.在R中,可以使用双引号,或单引号表示字符,函数nchar用于获得字符串中的字符数量: > s='read' > nc ...
- Python基础学习参考(五):字符串和编码
一.字符串 前面已经介绍过字符串,通过单引号或者双引号表示的一种数据类型.下面就再来进一步的细说一下字符串.字符串是不可变的,当你定义好以后就不能改变它了,可以进一步的说,字符串是一种特殊的元组,元 ...
随机推荐
- 如何写Emit代码
写Emit代码也不是想象中的那么复杂,基本过程就是先手工写好C#代码,编译得到Exe或者Dll,然后用ILDASM或反编译工具,得到IL代码,最后就是看着IL代码,用Emit一个个对应发出代码,就行了 ...
- 删除或修改本地Git保存的账号密码
win10 系统下进入 控制面板 > 用户帐户 > 管理你的凭据 选择 [Windows 凭据] git 保存的用户信息在普通凭据列表里 >>编辑>>>完成
- Educational Codeforces Round 2
600A - Extract Numbers 20171106 字符串处理题,稍微注意点细节就能水过 #include<stdlib.h> #include<stdio.h&g ...
- MySql的主从复制以及读写分离详解
MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中, ...
- 泡泡一分钟:Cubic Range Error Model for Stereo Vision with Illuminators
Cubic Range Error Model for Stereo Vision with Illuminators 带有照明器的双目视觉的三次范围误差模型 "链接:https://pan ...
- 使用阿里云公网ip建立bind,监听客户端连接失败
bind: Cannot assign requested address 网上的众多答案不能解决我的问题 通过ping可以ping通,但是 使用bind代码,这个ip就是不能用的, 使用 ifcon ...
- Codeforces 584 - A/B/C/D/E - (Done)
链接:https://codeforces.com/contest/584 A - Olesya and Rodion - [水] 题解:注意到 $t$ 的范围是 $[2,10]$,对于位数小于 $2 ...
- 记录常用的adb命令
1.启动adb服务 adb start-server 2.关闭服务 adb kill-server 3.进入shell环境 adb shell 4.安装应用 adb install -r xxx.ap ...
- 5.0-uC/OS-III时间管理
1.时间管理 uC/OS-III为用户提供了与时间管理相关的服务. 在uC/OS-III中设置了能提供时基中断的中断源.该中断源提供 10Hz 到 1000Hz 之间的中断(需设置OS_CFG_APP ...
- 【English】【托业】【四六级】写译高频词汇
大家都知道,四六级翻译每次考的话题不可能原句直接重复,但是,在研究了近几年的四六级真题后,我们惊奇地发现: 写译词汇在重复考! 写译词汇在重复考! 写译词汇在重复考! 因此,小编为大家整理了四六级写译 ...