-2

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. 0 will be considered as even index. 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. Its giving the error:

solution.cc: In function '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:20: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations] gets(g[i].s); ^

/ccQXNkYE.o: In function main': solution.cc:(.text.startup+0x4e): warning: thegets' function is dangerous and should not be used.

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;
}
BeyelerStudios
  • 4,243
  • 19
  • 38
Rajat
  • 19
  • 1
  • 8

2 Answers2

0

The function gets() is dangerous and not allowed by the platform. 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. The gets function doesn't know that. 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() also asks for the amount of chars it should copy. In your case it should be 999, because you don't want to "override" the terminator. The 1000. char will be your terminator.

However this is the C approach and not C++.

Also what happens if someone enters an number greater than 10? Your program would crash.

The following code would avoid the char[] completely and allow you to work with the std::string of C++.

#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);
}
noCma
  • 34
  • 4
  • 1
    People always say "don't use gets, use fgets. In fact this simple advice doesn't help, instead of crashing on overlong input, the program silently truncates the line, then exhibits defined but incorrect behaviour. Which is often worse than crashing, depending on exactly what you are trying to do. – Malcolm McLean Oct 02 '16 at 20:03
  • True. This is why I added code which doesn't use gets or fgets. The truncation could be a nasty source for errors or unexpected behavior. Thanks for pointing this out. – noCma Oct 02 '16 at 20:15
  • Thank you very much .... it solved the problem. I used string type data structure to store strings. – Rajat Oct 03 '16 at 17:51
-2

Some platforms don't allow gets() as its dangerous. instead use

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

to input a string including spaces or fgets() or cin.getline() for that case.

Edit :

As my fellow mates said a better version would be to use cin.getline() rather than any other function stated above.

Karan Nagpal
  • 51
  • 2
  • 8