简体   繁体   English

C ++中字符串中的偶数和奇数索引字符

[英]Even and odd indexed characters in a string in C++

Given a string, S, of length N that is indexed from 0 to N-1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line.给定一个长度为 N 的字符串 S,索引从 0 到 N-1,将其偶数索引和奇数索引字符作为 2 个空格分隔的字符串打印在一行上。 0 will be considered as even index. 0 将被视为偶数索引。 Number of input strings can be T from 1 to 10. The program is giving correct results in codeblocks (offline) but not on HackerRank platform.输入字符串的数量可以是从 1 到 10 的 T。该程序在代码块(离线)中给出了正确的结果,但在 HackerRank 平台上却没有。 Its giving the error:它给出了错误:

solution.cc: In function 'int main()': solution.cc:在函数“int main()”中:

solution.cc:22:9: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] gets(g[i].s); solution.cc:22:9: 警告: 'char* gets(char*)' 已弃用(在 /usr/include/stdio.h:638 处声明)[-Wdeprecated-declarations] gets(g[i].s) ; ^ ^

solution.cc:22:20: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] gets(g[i].s); solution.cc:22:20: 警告: 'char* gets(char*)' 已弃用(在 /usr/include/stdio.h:638 处声明)[-Wdeprecated-declarations] gets(g[i].s) ; ^ ^

/ccQXNkYE.o: In function main': solution.cc:(.text.startup+0x4e): warning: the gets' function is dangerous and should not be used. /ccQXNkYE.o: 在函数main': solution.cc:(.text.startup+0x4e): warning: the gets 函数是危险的,不应使用。

My code is:我的代码是:

#include <cstdio>
#include <iostream>

struct str {
    char s[10000];
};

int main() {
    using namespace std;
    int T; 
    cin >> T;
    fflush(stdin);
    str g[10];
    for(int i = 0; i < T; ++i) {
        gets(g[i].s);
        fflush(stdin);
    }
    for(int t = 0; t < T; ++t) {
        int j = 0;
        while(j < strlen(g[t].s)) {
            if(j % 2 == 0)
                cout << g[t].s[j];
            ++j;
        }
        cout << " ";
        int k = 0;
        while(k < strlen(g[t].s)) {
            if(k % 2 == 1)
                cout << g[t].s[k];
            ++k;
        }
        cout << endl;
    }
    return 0;
}

The function gets() is dangerous and not allowed by the platform.函数gets() 是危险的,平台不允许。 warning: the gets' function is dangerous and should not be used. The platform even tells you that.平台甚至告诉你。

Why is the function dangerous?为什么这个函数是危险的? Simple.简单的。 It can lead to an bufferoverflow.它可能导致缓冲区溢出。 You defined your char[] to have 1000 chars including the terminator.您将 char[] 定义为包括终止符在内的 1000 个字符。 The gets function doesn't know that. get 函数不知道这一点。 It will happily copy anything the user entered into the array, even if it doesn't fit.它会很高兴地复制用户输入到数组中的任何内容,即使它不适合。 Anything behind your array, will be overwritten with the excess text.数组后面的任何内容都将被多余的文本覆盖。 This can overwrite other variables or even includes malicious code, which could be executed.这可以覆盖其他变量,甚至包括可以执行的恶意代码。 The best case would be a crash of your program.最好的情况是你的程序崩溃。

It would be better to use fgets() .最好使用fgets() fgets() also asks for the amount of chars it should copy. fgets()还询问它应该复制的字符数量。 In your case it should be 999, because you don't want to "override" the terminator.在您的情况下,它应该是 999,因为您不想“覆盖”终止符。 The 1000. char will be your terminator. 1000. char 将是你的终结者。

However this is the C approach and not C++.然而,这是 C 方法而不是 C++。

Also what happens if someone enters an number greater than 10?如果有人输入大于 10 的数字会发生什么? Your program would crash.你的程序会崩溃。

The following code would avoid the char[] completely and allow you to work with the std::string of C++.以下代码将完全避免使用 char[] 并允许您使用 C++ 的std::string

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

int main()
{
    std::vector<std::string> inputs;
    int strings; // Your T
    std::cin >> strings;

    if (strings < 1 || strings > 10) {
        std::cout << "Error" << std::endl;
        exit(1);
    }

    for (int i = 0; i < strings; i++) {
        std::string temp;
        std::cin >> temp;
        inputs.push_back(temp);
    }

    // Do your reverse stuff.

    exit(0);
}

Some platforms don't allow gets() as its dangerous.有些平台不允许gets() 是危险的。 instead use而是使用

scanf(" %[^n]s",string_name);

to input a string including spaces or fgets() or cin.getline() for that case.在这种情况下输入一个包含空格或 fgets() 或 cin.getline() 的字符串。

Edit :编辑 :

As my fellow mates said a better version would be to use cin.getline() rather than any other function stated above.正如我的同伴所说,更好的版本是使用 cin.getline() 而不是上述任何其他函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM