Coversion problem

how to convet string “1234” to integer 1234 and vice versa?

Hey Amit, there are many ways to convert string to integer and vice versa in c++.

  • String to integer
  1. using stringstream class

    string str = “1234”;
    stringstream st(str);
    int num = 0;
    st >> num;

  2. using stoi()

      string str = "1234"; 
      int num = stoi(str);
    
  • integer to string
  1. using itoa()

     int num = 1234;
     char *intStr = itoa(num);
     string str = string(intStr);
    
  2. auto s = std::to_string(1234);

If you want to explore more about these functions you can refer this.