How to convert string of characters to their respective int values? - c++

I'm trying to convert a string of characters into their ASCII int values. However I cannot get this to work for one even one character in the string. I would expect a result of 72 when entering 'H', but it returns a 0 (the same for every character I've tried).
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
const char * b = a.c_str();
int c = atoi(b);
cout << int(c) << endl;
}
Thanks in advance.

atoi parses the C-string interpreting its content as an integral number, i.e.
int i = atoi("123"); // i = 123
You don't want this: you want to know the ASCII value of every single character of the input string. To figure this out, you can use this code snippet:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for(int c: a)
cout << c << '\n';
}

I'm not quite used to the string library but simply type:
cout<<(int)a[pozition];
You can place that in a for like this.
for(int i=0;i<a.length();i++)
cout<<(int)a[i]<<endl;

You can just cast each character to an int
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for (int i = 0; i < a.length(); i++) {
cout << (int) a[i] << endl;
}
}

Related

Is there a way to search an element of a vector<String>, then check if it contains a particular char, if it does print it

The final element in the vector is the char to search for.
Here’s my code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<string> words;
string in;
while(cin>>in)
{
words.push_back(in);
}
int size = words.size()
string check = words.at(size-1);
}
I tried your code and the loop was infinite. Try asking a set number of words then use the find method for each string. Here's an example.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> words;
string in;
cout << "Enter 5 words: \n";
for (int i{0}; i < 5; ++i)
{
cout << "Word: ";
cin >> in;
words.push_back(in);
}
cout << "What character do you want to search for? ";
char c;
cin >> c;
for (auto word : words)
{
if (word.find(c) != string::npos)
{
cout << "Character \"" << c << "\" found in " << word << endl;
}
}
return 0;
}
Edit: I noticed that you tried to use int to store the size for the string. Use size_t instead. I also think you meant to use the length method of the string, which also returns size_t.

How to assign char to string object in c++?

i am trying to convert the string into capital letter string by assigning single char's to string like this:-
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.reserve(a.size()+1);
for(int i=(a.size()),i1=0;1;i1++)
{
if(b[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
a[i1]='\0';
break;
}
}
cout << b <<endl;
}
every when run a.out by ./a.out ,Only endl gets prints
here is sample run:-
$ ./a.out
play clash royale
$
What is wrong in my program?? How can I assign single char to string??
There are some issues with your program. The main one is probably the diference between string reserve and string resize. What you want in your program is already had a string of a.size() length, so, use b.resize(a.size()).
A working version is bellow (there are better ways to write this, just being most consistent with OP proposal):
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
int main()
{
string a;
getline(cin,a);
string b;
b.resize(a.size());
for(int i1=0; i1 < a.size();i1++)
{
if(a[i1]!='\0')
b[i1]=(char)toupper(a[i1]);
else
{
b[i1]='\0';
break;
}
}
cout << b <<endl;
}

how to leave space between string in C++

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string a;
string b = "hey";
cin >> a;
if (a == b) {
cout << "hello \n";
}
int z = 40;
string x = "";
string y = "weather";
cin >> x;
if (x == y) {
cout << "the temp is " << z << endl;
}
return 0;
}
it works correctly but once I change string y to a sentence that have spaces in it it does not work for example :
string y = "this is a sentence";
Use getline() function or you can also use gets() function.
If you are using STL string then,
getline(cin,str); /*it will work */
If you are not using STL string then,
#include <cstdio>
char *gets( char *str );

converting uppercase letter to lowercase and vice versa for a string

I am trying to convert a string characters from uppercase to lowercase. There is no compilation error but I am still getting same output as input:
#include <iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
int main() {
char a[100];
cin>>a;
for(int i =0;a[i];i++){
if(islower(a[i])){
toupper(a[i]);
}
else if(isupper(a[i])){
tolower(a[i]);
}
}
cout<<a;
return 0;
}
std::toupper , std::tolower functions do not work in-place. They return the result, so you have to assign it to a[i] again:
char a[100];
std::cin>>a;
for(std::size_t i =0;a[i];i++){
if(std::islower(a[i])){
a[i]=std::toupper(a[i]);// Here!
}
else if(std::isupper(a[i])){
a[i]=std::tolower(a[i]);// Here!
}
}
std::cout<<a;
You could use the transform function from the Standard Library with a lambda function that returns the uppercase or lowercase character of a given character.
#include <algorithm>
#include <iostream>
using namespace std;
int main
{
string hello = "Hello World!";
transform(hello.begin(), hello.end(), hello.begin(), [](char c){
return toupper(c);})
cout << hello << endl;
}
This would output HELLO WORLD!. You can imagine doing the same thing for lowercase
Here's a solution I found by calling another char variable "charConvert" and setting it equal to the converted character.
#include <iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
int main() {
char a[100];
cin >> a;
char charConvert;
for (int i = 0; a[i] ; i++) {
if (isupper(a[i])) {
charConvert = tolower(a[i]);
}
else if (islower(a[i])) {
charConvert = toupper(a[i]);
}
}
cout << charConvert << endl;
return 0;
}

c++ istringstream() function converting string to int raises error

I have a string of digits. I am trying to print it as an int type each single digit in the string using istringstream. It works fine if pass whole string as argument to conversion function in main but if I pass it by index, it raises error.
How to make this code work using index to print each single digit in string array as an int.
Here is my code.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int strToNum(string str)
{
istringstream ss(str);
int n;
ss>>n;
cout<<n;
}
int main()
{
string str = "123";
for(int i=0; i<str.length(); i++)
//strToNum(str); Works fine
strToNum(str[i]); //raises error
}
str[i] is a char, while the strToNum expects a string, hence the type error.
It raises error because str[i] is a char
however , strToNum(string str) excepts a string
Try this :
for(int i=0; i<str.length(); i++)
strToNum(string(1,str[i])); //Convert char to string
See here
Others have explained your error. This is how you could make it work:
strToNum( std::string(1, str[i]) );
But I'd do this instead:
for(int i=0; i<str.length(); i++)
cout << str[i] - '0';
But ask yourself if you really need this. Are you interested in the value or the representation? If the latter, just print chars.
You don't need istringstream at all.
int strToNum(char ch)
{
cout << ch;
}
Actually I use a template function to perform this task, which is a more useful way to write the function that originated this thread ( because this single function can convert a string to any type of number: int, float, double, long double ):
#include "stdafx.h"
#include <string>
#include <iostream>
#include <Windows.h>
#include <sstream>
#include <iomanip>
using namespace std;
template <typename T>
inline bool StrToNum(const std::string& sString, T &tX)
{
std::istringstream iStream(sString);
return (iStream >> tX) ? true : false;
}
void main()
{
string a="1.23456789";
double b;
bool done = StrToNum(a,b);
cout << a << endl;
cout << setprecision(10) << b << endl;
system ("pause");
}
setprecision(10) ( iomanip ) is required otherwise istringstream will hide some decimals