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

时间:2023-03-09 08:12:38
[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");
}

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

2.字符串和整数转化

预备知识

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

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

字符串转整数

#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;
}

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

整数和字符串互转

#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");
}

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

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

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");
}

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

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");
}

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

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");
}