C++ Tutorials

C++ Tutorials

For char, string or short to int, you just need to assign the value.

char ch = 19;
int in = ch;
Same to int64.

long long lo = ch;
All values will be 19

//Convert char to int in c++ to get correct values of numbers
char x='1'; //ascii value=49
int xx=x-'0' //49-48==1

char a='C'; //67
char aa=a-'A' //67-65=2 means third char of Alphabet 

There are various ways in which one can convert char to its int value.

  1. Using ASCII values: This method uses TypeCasting to get the ASCII value of the given character. From this ASCII value, the respective integer is calculated by subtracting it from the ASCII value of 0.
  2. Using String.
  3. Using Character.

String to int C++

C++ string to int Conversion We can convert string to int in multiple ways.

C++11 there are some nice new convert functions from std::string to a number type.

So instead of

atoi( str.c_str() )

you can use

std::stoi( str )

where str is your number as std::string.

There are version for all flavours of numbers: long stol(string)float stof(string)double stod(string)

Char to string C++

std::string has a constructor for this:

const char *s = "Hello, World!";
std::string str(s);

or

All of

std::string s(1, c); std::cout << s << std::endl;
and

std::cout << std::string(1, c) << std::endl;
and

std::string s; s.push_back(c); std::cout << s << std::endl;

How to convert characters of a string to opposite case. Let’s understand the problem. Given a string,we need to convert the characters of the string into opposite case i.e. if a character is lower case than convert it into upper case and vice-versa.

Examples given below will simplify. If given a string like this GeeksforGeeks, we will convert each character to opposite case, ouput will be the same string but each character will have opposite case as the case in input string.

In this example,F & E are in upper case in input while they are in lower case in otput string, whereas other characters were lower case in input and are in upper case in output, similarly in second example, all characters are in lower case and are converted to upper case in output.

We will use ASCII values to convert each character in a string. Capital letters have values from 65 to 90, while LowerCase letters have values from 97 to 122. Now to convert case of a character we need to manipulate their ASCII values.

In ASCII values small a – capital A is 32,i.e. Capital A = small a – 32 and Small a = Capital A + 32. Hence we can convert LowerCase to uppercase by subtracting 32 from ASCII values, similarly by adding 32 to ASCII value, UpperCase character can be converted to LowerCase.

We can also use inbuilt functions in C++, for conversions. Understanding the C++ Code. convertOpposite takes a string as input and has returntype void and converts each character of input string to it’s opposite case. First we store length of input string in integer ln by using str.length() function, which returns length of string.

Next we will iterate through each character of this string using this for loop which goes from 0 to l-1. If str[i] i.e. the present character is in lower case as checked by this if statement str[i] >= small a and <= small z.

Then we subtract 32 from it’s ASCII value for conversion to upper case. else if character is in UpperCase i.e. ASCII value falls between the ASCII value of capital A and capital Z, then we convert it to a lower case by adding 32 to its ASCII value. Now each character of str is in its opposite case.

String char to int C++

There are two common methods to convert strings to numbers:

  • Using stringstream class or sscanf() stringstream() : This is an easy way to convert strings of digits into ints, floats or doubles. …
  • String conversion using stoi() or atoi() stoi() : The stoi() function takes a string as an argument and returns its value.

Using the stringstream class

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

int main() {
  string str = "100"; // a variable of string data type
  
  int num; // a variable of int data type

  stringstream ss;  
  ss << str;  
  ss >> num;  

  cout << "The string value is " << str << endl;  
  cout << "The integer representation of the string is " << num << endl;  
}

Using stoi()

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

int main() {
  // 3 string examples to be used for conversion
  string str_example_1 = "100";
  string str_example_2 = "2.256";
  string str_example_3 = "200 Educative";

  // using stoi() on various kinds of inputs
  int int_1 = stoi(str_example_1);
  int int_2 = stoi(str_example_2);
  int int_3 = stoi(str_example_3);

  cout << "int_1 : " << int_1 << endl;
  cout << "int_2 : " << int_2 << endl;
  cout << "int_3 : " << int_3 << endl;
}

Using atoi()

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

int main() {
  // 3 string examples to be used for conversion
  const char* str_example_1 = "100";
  const char* str_example_2 = "2.256";
  const char* str_example_3 = "200 Educative";

  // using stoi() on various kinds of inputs
  int int_1 = atoi(str_example_1);
  int int_2 = atoi(str_example_2);
  int int_3 = atoi(str_example_3);

  cout << "int_1 : " << int_1 << endl;
  cout << "int_2 : " << int_2 << endl;
  cout << "int_3 : " << int_3 << endl;
}

Get $100 to try DigitalOcean - Try for Free for 60 Days

X