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[50] = "hello yincheng hello cpp hello linux";
char findstr[10] = "cpp";
char insertstr[10] = "hello c";
char *p = strstr(allstr, findstr);//查找字符串
if(p==NULL)
printf("空指针,意味着没有找到n");
else
{
printf("找到%c,地址%pn", *p, p);
char temp[30];
strcpy(temp, p + 4);//从p+4开始拷贝
printf("%sn", temp);
*(p + 4) = ' ';
strcat(allstr, insertstr);
strcat(allstr, temp);
printf("%sn",allstr);
}
system("pause");
}
2.字符串和整数转化
预备知识
void main() {
printf("%d,%cn", 1, 1);//1,编号为1的字符
printf("%d,%cn",'1','1');//字符‘1’的编号49,字符‘1’
printf("%dn",'1'-1);//48
system("pause");
}
字符串转整数
#include<stdio.h>
#include<stdlib.h>
int tonum(char *str) {
char *istr = str;//保留副本
int num = 0,sum=0;
while (*str)
{
if (*str<'0' || *str>'9')
return -1;
str++;
num++;//计数,判断有多少位
}
//str已经到了末尾
printf("%dn",num);
for (int i = 0; i < num; i++)
{
//int wei = str[i] - 48;//这句会导致结果错误,因为在上面的while循环中,str的地址已经发生了变化
int wei = istr[i] - 48;
for (int j = i+1; j < num; j++)
{
wei *= 10;
}
sum += wei;
}
return sum;
}
void main() {
char str[10] = "123456789";
int num = tonum(str);
printf("%dn", num);
system("pause");
}
tonum函数另解(更简单)
int tonum(char *str) {
char *istr = str;//保留副本
int num = 0,sum=0;
while (*str)
{
if (*str<'0' || *str>'9')
return -1;
str++;
num++;//计数,判断有多少位
}
//str已经到了末尾,继续使用str会出现数据错误
printf("%dn",num);
for (int i = 0; i < num; i++)
{
sum *= 10;
int wei = istr[i] - 48;
sum += wei;
}
return sum;
}
整数和字符串互转
#include<stdio.h>
#include<stdlib.h>
int tonum(char *str) {
char *istr = str;//保留副本
int num = 0,sum=0;
while (*str)
{
if (*str<'0' || *str>'9')
return -1;
str++;
num++;//计数,判断有多少位
}
//str已经到了末尾,继续使用str会出现数据错误
printf("%dn",num);
for (int i = 0; i < num; i++)
{
sum *= 10;
int wei = istr[i] - 48;
sum += wei;
}
return sum;
}
void tostr(int num,char *str) {
int wei = 0;
for (int inum = num; inum; inum /= 10)
wei++;
printf("wei=%dn", wei);
for (int i = wei - 1; num; num /= 10, i--)
{
//printf("%d,%dn", num%10,i);
str[i] = num % 10 + 48;
}
}
void main() {
int num = 1234567;
char str[10] = { 0 };//编号为0的字符
tostr(num,str);
printf("%sn", str);
system("pause");
}
3.删除字符
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main() {
char str[50] = "hello yincheng,hello c,hello cpp";
char ch = 'c';//要删除的字符
char last[50] = { 0 };//创建一个空字符串
char *p = str;
int i = 0;
while (*p)
{
if (*p != ch) {
last[i] = *p;
i++;
}
p++;
}
printf("%sn", last);
system("pause");
}
4.模拟银行密码验证
输入三次错误就锁定,防止暴力穷举
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main() {
char pass[30] = "password";
for (int i = 0; i < 3; i++)
{
char input[30];
gets_s(input);//VS2015采用c11新标准,使用gets_s而不是gets:输入字符串并初始化
if (strcmp(input, pass) == 0) {
printf("密码正确n");
break;
}
else
printf("输入错误,你还有%d次机会n", 2 - i);
if(i==2)
printf("密码输入三次都失败,账户已被锁定n");
}
system("pause");
}
5.字符串输入注意事项
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void main0() {
char str[30];
//scanf会将空格,回车,换行,换页,制表符当做终止符停止数据输入
scanf("%s", str);
printf("%sn",str);
char str1[30];
scanf("%s", str1);
printf("%sn", str1);
system("pause");
}
void main() {
char str[30];
gets_s(str);//接收空格和制表符,遇到换行结束
printf("%sn", str);
system("pause");
}
原文链接: https://www.cnblogs.com/little-monkey/p/7531602.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/259987
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!