0

Please tell me what is wrong in my approach.

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

string datatype(string x) {
    for (int k = 0; k < strlen(x.c_str());) {
        for (int i = 0; i < 10; i++) {
            char z = i;
            if (x[k] == z) {
                k++;
            }
            else {
                return "string";
            }
        }
    }
    return "int";
}

int main() {
    string inp;
    cin >> inp;

    cout << datatype(inp);
}

Whatever i enter, it always returns "string". I have seen the other questions posted here but please tell me what is wrong in my approach.

3
  • 2
    Why strlen(x.c_str()) instead of x.length() or x.size()? Commented Jun 10, 2019 at 11:32
  • 6
    And in general it seems you could need some extra time reading your text-books. I don't know any character encoding where e.g. 1 == '1' is true. For ASCII (the most common encoding) it's definitely not true. Commented Jun 10, 2019 at 11:33
  • 2
    I also recommend that you check out std::isdigit and std::all_of. Or perhaps std::stoi. Commented Jun 10, 2019 at 11:34

2 Answers 2

4

The standard library has the isdigit function that tells you if the char is a digit.

Here you check that each char of your input is a digit, as soon as a character that is not a digit is found string is returned, else int.

For example 1234 returns int, sdf returns string.

string datatype(string str) {
    for (unsigned char c : str) {
        if (!isdigit(c)) {
            return "string";
        }
    }
    return "int";
}

Edit: This solution also handles leading - and +. It will return int for -10 and +10 but returns string for +1+1 or -10+10.

string datatype(string str) {
    if (str.size() == 1) {
        return isdigit(str[0]) ? "int" : "string";
    }

    bool isInt = true;
    for (int i = 1; i < str.size(); i++) {
        isInt = isInt && isdigit(static_cast<unsigned char>(str[i]));
        if (!isInt) {
            break;
        }
    }

    if (isInt) {
        unsigned char c = str[0];
        isInt = isInt && (c == '-' || c == '+' || isdigit(c));
    }

    return isInt ? "int" : "string";
}
Sign up to request clarification or add additional context in comments.

6 Comments

One minor nitpick. Better to handle negative numbers as well. isdigit('-') returns false.
Please note specifically in the answer that the cast to unsigned char is required
@HolyBlackCat Look at the loop preamble (see? this needs to be explicitly called out for clarity!)
Then for completeness, you would want to check further if the valid hexadecimal with isxdigit() and in addition to the leading '-', a leading '+' is also the valid start of a signed value.
@SilvanoCerza since the question explicitly specifies "integers", decimal points needn't be handled for completeness.
|
0

First of all Include (cstring) as header since x.c_str is not in iostream and string. Then, When you are doing char z=i; here you are not storing the character equivalent of i in z but the ascii value of i. Next, You are returning string at the first mismatch between i and x[k]. You should return string if you cannot find a match with any of the possible 10 digits.

You can have a look at the modified code.

#include <iostream>
#include<cstring>
#include <string>
using namespace std;

string datatype(string x) {


    for (int k = 0; k < strlen(x.c_str());k++) {
         int flag=0;
        for (int i = 0; i < 10; i++) {
           // char z = i;
            if ((x[k]-'0') == i || (k==0 && x[k]=='-')) {
                flag=1;
               break;
            }

        }
          if(flag==0)
          return "string";


    }
    return "int";
}

int main() {
    string inp;
    cin >> inp;
   cout << datatype(inp);
}

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.