How to convert string to integer using stoi()?

Viewed 293

If there are more than 3 letters together (i.e. next to each other) from the given Alphabet Set: {a,g,w,k,l}, then your string is “BAD”. If a number is repeated more than three times, then your string is “BAD”. Print "1” if string is GOOD, else print "0".

For the first condition, I am able to do it. But for the second condition, I am having problem with converting a string to an int. I tried using stoi() but I think I am not using it correctly.

#include<bits/stdc++.h>

using namespace std;

int main() {

  int t;
  cin >> t;
  while (t--> 0) {
    string s;
    cin >> s;
    int ccount = 0;

    int arr[10] = {0,0,0,0,0,0,0,0,0,0};
    bool b = true;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'a' || s[i] == 'g' || s[i] == 'w' || s[i] == 'k' || s[i] == 'l') {
        ccount++;
        if (ccount >= 3) {
          b = false;
          break;
        }
      } else if (isdigit(s[i])) {
        ccount = 0;
        int num = stoi( & s, sizeof(s) * i, 10);
        arr[num]++;
        int k = 0;
        for (k = 0; k < 10; k++) {
          if (arr[k] >= 3) {
            b = false;
            break;
          }
        }
      } else {
        ccount = 0;
      }

    }
    if (b == true) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }

  }
  return 0;
}

Input:

3
qw2uha
awkl5
y2y2y2y2

Output:

1
0
0

EDIT: ASCII conversion works very well.

I have a not so important problem, I know we can't directly compare integers of different signedness, but still I am curious.

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

int main() {

  int t;
  cin >> t;
  while (t--> 0) {
    string s;
    cin >> s;
    int ccount = 0;
    int arr[10] = {0,0,0,0,0,0,0,0,0,0};
    bool b = true;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'a' || s[i] == 'g' || s[i] == 'w' || s[i] == 'k' || s[i] == 'l') {
        ccount++;
        if (ccount >= 3) {
          b = false;
          break;
        }
      } else if (isdigit(s[i])) {
        ccount = 0;
        int num = s[i] - '0';
        arr[num]++;
        int k = 0;
        for (k = 0; k < 10; k++) {
          if (arr[k] > 3) {
            b = false;
            break;
          }
        }
      } else {
        ccount = 0;
      }
    }
    if (b == true) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}

I am getting this warning :

14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'unsigned int'} [-Wsign-compare]
   14 |     for (int i = 0; i < s.size(); i++) {
      |                     ~~^~~~~~~~~~
]

Is there any way to fix the warning?

1 Answers

You can convert a single ASCII character representing a digit like this:

int num = s[i] - '0';

That works because inside the string, each character is represented by an ASCII code - they're listed in the table below. The character '0' has ASCII code 48 decimal and other digits increment from there. When you do s[i] - '0' and say s[i] is '5', it calculates 53 - 48 = 5. ` enter image description here


std::stoi() is only useful if you have a string starting with (optional whitespace) then a number, e.g. "1x" (stoi would extract the number 1), " 123" (123), or "44kx2" (44). As your s string may have characters before or afterwards, you could use s.substr(i, 1) to extract the single digit as a string, then use std::stoi() on that, but the ASCII conversion above is faster/ simpler/ more-direct.

Related