[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[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("空指针,意味着没有找到
");
    else
    {
        printf("找到%c,地址%p
", *p, p);
        char temp[30];
        strcpy(temp, p + 4);//从p+4开始拷贝
        printf("%s
", temp);
        *(p + 4) = '';
        strcat(allstr, insertstr);
        strcat(allstr, temp);
        printf("%s
",allstr);
    }
    system("pause");
}

2.字符串和整数转化

预备知识

void main() {
    printf("%d,%c
", 1, 1);//1,编号为1的字符
    printf("%d,%c
",'1','1');//字符‘1’的编号49,字符‘1’
    printf("%d
",'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("%d
",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("%d
", 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("%d
",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("%d
",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=%d
", wei);
    for (int i = wei - 1; num; num /= 10, i--)
    {
        //printf("%d,%d
", num%10,i);
        str[i] = num % 10 + 48;
    }
}

void main() {
    int num = 1234567;
    char str[10] = { 0 };//编号为0的字符
    tostr(num,str);
    printf("%s
", 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("%s
", 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("密码正确
");
            break;
        }
        else
            printf("输入错误,你还有%d次机会
", 2 - i);
        if(i==2)
            printf("密码输入三次都失败,账户已被锁定
");
    }
    system("pause");
}

5.字符串输入注意事项

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

void main0() {
    char str[30];
    //scanf会将空格,回车,换行,换页,制表符当做终止符停止数据输入
    scanf("%s", str);        
    printf("%s
",str);

    char str1[30];
    scanf("%s", str1);
    printf("%s
", str1);

    system("pause");
}

void main() {
    char str[30];
    gets_s(str);//接收空格和制表符,遇到换行结束
    printf("%s
", str);

    system("pause");
}
原文地址:https://www.cnblogs.com/little-monkey/p/7531602.html