C++字符串输入处理

空白符的正确处理

空白符:指回车和空格。

cin不会丢弃空白符,但是 >> 会跳过空白符。于是该行中最后输入的回车符就会被保存在cin的缓冲区中,在下次输入时会首先从缓冲区中取出这个空白符 ,这就会影响下次输入。所以一般在涉及到多行字符串输入时:就需要使用getchar()将这个空白符取出来,让它不会干扰后续输入

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    cout << "n:" << n << endl; 
    int ip[4] = {-1};
    int mask[4] = {-1};
    // char delimiter='A';
    // char po='B';
    char delimiter;
    char po;
    getchar();
    for (int i = 0; i < n; i++) {
        string str;
        getline(cin, str);
        cout << "str:" << str << endl;
        stringstream ss(str);
        ss >> ip[0] >> delimiter >> ip[1] >> delimiter >> ip[2] >> delimiter >> ip[3] >>
           po
           >> mask[0] >> delimiter >> mask[1] >> delimiter >> mask[2] >> delimiter >>
           mask[3];
        cout << ip[0] << delimiter << ip[1] << delimiter << ip[2] << delimiter << ip[3]
             <<
             po
             << mask[0] << delimiter << mask[1] << delimiter << mask[2] << delimiter <<
             mask[3] << endl;;
    }
}

须处理的输入

4
10.70.44.68~255.254.255.0
1.0.0.1~255.0.0.0
192.168.0.2~255.255.255.0
19..0.~255.255.255.0

输出

n:4
str:10.70.44.68~255.254.255.0
10.70.44.68~255.254.255.0
str:1.0.0.1~255.0.0.0
1.0.0.1~255.0.0.0
str:192.168.0.2~255.255.255.0
192.168.0.2~255.255.255.0
str:19..0.~255.255.255.0
19.0.0.2~255.255.255.0
基本的字符处理函数

以下函数的声明在头文件

  1. isalpha

isalpha()用来判断一个字符是否为字母,如果是字符则返回非零,否则返回零。

cout<<isalpha(‘a’); //返回非零
cout<<isalpha(‘2’); //返回0

2.isalnum

isalnum()用来判断一个字符是否为数字或者字母,也就是说判断一个字符是否属于az||AZ||0~9

cout<<isalnum(‘a’); //输出非零
cout<<isalnum(‘2’); // 非零
cout<<isalnum(‘.’); // 零

3.islower

islower()用来判断一个字符是否为小写字母,也就是是否属于a~z。

cout<<islower(‘a’); //非零
cout<<islower(‘2’); //输出0
cout<<islower(‘A’); //输出0

4.isupper

isupper()和islower相反,用来判断一个字符是否为大写字母。

cout<<isupper(‘a’); //返回0
cout<<isupper(‘2’); //返回0
cout<<isupper(‘A’); //返回非零

5.tolower

tolower()函数是把字符串都转化为小写字母

string str= “THIS IS A STRING”;
for (int i=0; i <str.size(); i++)
str[i] = tolower(str[i]);

5.toupper

toupper()函数是把字符串都转化为小写字母

string str= “hahahahaha”;
for (int i=0; i <str.size(); i++)
str[i] = toupper(str[i]);

字符串处理函数

判断 IP是否违法

 bool judge_ip(string ip){
    int j = 0;
    istringstream iss(ip);
    string seg;
    while(getline(iss,seg,'.'))
        if(++j > 4 || seg.empty() || stoi(seg) > 255)
            return false;
    return j == 4;
}

将字符串从.或者:分割开

    vector<string> split(string s, string spliter) { 
        vector<string> res;
        int i;
        //遍历字符串查找spliter
        while((i = s.find(spliter)) && i != s.npos){ 
            //将分割的部分加入vector中
            res.push_back(s.substr(0, i)); 
            s = s.substr(i + 1);
        }
        res.push_back(s);
        return res;
    }
    
c强制类型转换
#include <stdio.h>

int main() {
    char c = 'A'; // 字符变量c,其ASCII值为65
    int i = (int)c; // 将字符c强制转换为int类型
    printf("The integer value of %c is %d\n", c, i);
    return 0;
}
cin cout 测试
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, a;

    string N;
    string str;
    cin >> N;
    while(getline(cin, str)) {
        cout << "str:" << str << endl; 
    }
    return 0;
}

输入

3

2
2
1

输出

str:
str:
str:2
str:2
str:1

如果只是 cin 整型数据, cin会自动跳过换行符。

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, a;

    string N;
    string str;
    // cin >> N;
    // while(getline(cin, str)) {
    //     cout << "str:" << str << endl; 
    // }

    cin >> n;
    cout << "n:" << n << endl;
    while(cin >> a){
        cout << "a:" << a << endl;
    }

    return 0;
}

输入:

3

2
2
1

输出:

n:3
a:2
a:2
a:1

算法题

提取最后一个单词的长度

计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)

#include <bits/stdc++.h>
using namespace std;

int main() {
    string ss;
    int pos = -1;
    while (getline(cin, ss)) {
        pos = ss.rfind(" ");
        cout << ss.size() - pos -1 << endl;
    }
}
// 64 位输出请用 printf("%lld")
进制转换

写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int a;
    while(cin >> hex >> a) {
        cout << a << endl;
    }
    return 0;
}

回文字符串

给定一个仅包含小写字母的字符串,求它的最长回文子串的长度。
所谓回文串,指左右对称的字符串。
所谓子串,指一个字符串删掉其部分前缀和后缀(也可以不删)的字符串

#include<bits/stdc++.h>
using namespace std;
int getLen(string str, int low, int high) {    //中心扩展法
    while(low>=0 && high<str.length() && str[low]==str[high]) {
        low--;
        high++;
    }
    return high-low-1;
}
int main() {
    string str;    //存放输入的字符串
    while(getline(cin, str)) {    //获取输入的一行字符串
        int length = str.length();    //length为字符串的长度
        if(length==0 || length==1) {    //如果字符串长度为小于等于1,直接输出字符串的长度
            cout<<length<<endl;
            continue;
        }
        else {    //如果字符串的长度大于1
            int len=0;    //len为最长回文子串的长度
            for(int i=0;i<length-1;i++) {    //循环计算每个中心点下的最长回文子串
                int x = getLen(str, i, i);    //单中心回文子串长度
                int y = getLen(str, i, i+1);    //双中心回文子串的长度
                len = max( max(x,y), len);
            }
            cout<<len<<endl;
        }
    }
    return 0;
}

需要注意的地方:

  • int len=0; len的初始化值 一定要赋值,否则会有随机性
  • for(int i=0;i<length-1;i++) i位置的上限,指向倒数第二字符的位置
hash表相关问题

明明生成了N个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, a;

    cin >> n;
    int num[1001] = {0};
    while(cin >> n){
        num[n] = 1; 
    }
    for(int i=0; i<1001; i++) {
        if(num[i] == 1) {
            cout << i << endl;
        }
    }

    return 0;
}



字符串切割

描述
•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;

•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(每个字符串长度小于等于100)

输出描述:
依次输出所有分割后的长度为8的新字符串

#include<bits/stdc++.h>
using namespace std;

int main() {
    string str;
    while(getline(cin, str)) {
        int len = str.size();
        int remainder = len % 8;
        if(remainder != 0) {
            str.append(8-remainder, '0');
        }
        for(int i=0; i<len; i+=8) {
            cout << str.substr(i,8) << endl;
        } 
    }
    return 0;
}
提取不重复的整数

输入一个 int 型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
保证输入的整数最后一位不是 0 。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int num;
    int arr[10] = {0};
    while(cin >> num) {
        while(num >0) {
            int index = num%10;
            if(arr[index]==0) {
                arr[index] = 1;
                cout << index;
            }
            num = num/10;
        }
        cout << endl;
    }

    return 0;
}

两个不超过100位的非负大整数的乘积

可参考:
https://blog.csdn.net/qq_45865697/article/details/106755313
https://leetcode.cn/problems/multiply-strings/solutions/372098/zi-fu-chuan-xiang-cheng-by-leetcode-solution/

#include <iostream>
#include <string>
#include <vector>

std::string multiply(std::string num1, std::string num2) {
    int m = num1.size();
    int n = num2.size();
    std::vector<int> result(m + n, 0);

    for (int i = m - 1; i >= 0; i--) {
        for (int j = n - 1; j >= 0; j--) {
            int mul = (num1[i] - '0') * (num2[j] - '0');
            int sum = mul + result[i + j + 1];

            result[i + j] += sum / 10;
            result[i + j + 1] = sum % 10;
        }
    }

    std::string res;
    for (int digit : result) {
        if (!(res.empty() && digit == 0)) {
            res.push_back(digit + '0');
        }
    }

    return res.empty() ? "0" : res;
}

int main() {
    std::string num1 = "123456789012345678901234567890";
    std::string num2 = "987654321098765432109876543210";
    
    std::string result = multiply(num1, num2);
    
    std::cout << "The product of the two large integers is: " << result << std::endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值