Int to ASCII char and char to ASCII number | C++ - c++

I must write short script which will change int to ASCII char and char to ASCII int. But i don't know how. I wrote this short code but something is wrong. I'm programming for half-year. Can someone write it working? It is my first time to use functions in c++
#include <iostream>
#include <conio.h>
using namespace std;
char toChar(int n) {
return n + '0';
}
int toInt(char c) {
return c - '0';
}
int main()
{
int number;
cout << "Int: ";
cin >> number;
cout << "ASCII: " << static_cast<char>(number);
getch();
return 0;
}

Thank You very much Guys, I have done it with shorter and working code.
#include <iostream>
using namespace std;
int main(){
int a=68;
cout<<char(a);
char c='D';
cout<<int(c);
return 0;
}

you could also use printf, so you don't need to create other function for converting the number.
int i = 64;
char c = 'a';
printf("number to ascii corresponding to %d is %c\n", i, i);
printf("ascii to number corresponding to %c is %d\n", c, c);
the output is gonna be
number to ascii corresponding to 64 is A
ascii to number corresponding to a is 97

May be you could start with the code below. If this is incomplete, could you please complete your question with test cases?
#include <iostream>
using namespace std;
char toChar(int n)
{ //you should test here the number shall be ranging from 0 to 127
return (char)n;
}
int toInt(char c)
{
return (int)c;
}
int main()
{
int number = 97;
cout << "number to ascii corresponding to " << number << " is " <<(char)number << " or " << toChar(number) <<endl;
char car='H';
cout << "ascii to number corresponding to " << car << " is " << (int)car << " or " << toInt(car) << endl;
return 0;
}
The output is:
number to ascii corresponding to 97 is a or a
ascii to number corresponding to H is 72 or 72

#include <iostream>
using namespace std;
char toChar(int n)
{
if (n > 127 || n < 0)
return 0;
return (char)n;
}
int toInt(char c)
{
return (int)c;
}
int main()
{
int number = 97;
cout << "char corresponding to number " << number << " is '" << toChar(number) << "'\n";
char car='H';
cout << "number corresponding to char '" << car << "' is " << toInt(car) << "\n";
return 0;
}
output:
char corresponding to number 97 is 'a'
number corresponding to char 'H' is 72'
Originally I thought you were just looking to convert the number:
You can simply add and substract '0', to char and from char:
char toChar(int n) {
return n + '0';
}
int toInt(char c) {
return c - '0';
}
If you just want to cast the type, read this

Related

how can I print ascii code value in c++ in this way?

I'd like to show each letter's ascii code
for example
Input: HelloWorld
Ascii Value: 72 + 101 + 108 ... = 1100
And here's my now-code
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
char str[32] = { 0 };
int value = 0, i;
cout << "Input: ";
cin >> str;
for (i=0;i<32;i++)
{
value += str[i];
}
cout << "Ascii Value:" << value << endl;
return 0;
}
I only can take the total value of ascii code such as 1100,
not every code value of each letters such as 7 + 11 + ... = 1100.
How can I fix it?
You should use a string for your input (it's c++, not c). Your for loop sums 32 characters, even if the user inputs a shorter string (the programm will read random values from memory). For conversion from int to char you can use stringstream. This results in
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string input;
std::stringstream sstr;
int value = 0;
std::cout << "Input: ";
std::cin >> input;
for (int i = 0; i < input.size(); i++) {
sstr << int(input[i]) << " + ";
value += input[i];
}
std::string str(sstr.str());
std::cout << "Ascii Value:" << str.substr(0, str.size() - 3) << " = " << value << std::endl;
return 0;
}

Display duplicate characters in a string

I wrote some code in C++ to display duplicate characters in a string, but if a character is repeated more than three times, the code prints the repeated character more than once.
For example if the string is aaaddbss, it should only print out ads but it prints aaads instead.
What am I doing wrong?
cout << " Please enter a string" << endl;
cin.getline(input, 100); // example input (ahmad wahidy) the output reads a a h a d instead of a h d
for (int i = 0;input[i]!='\0'; i++)
{
for (int j = i+1;input[j]!='\0'; j++)
{
if (input[i] == input[j])
{
cout << input[i] << " ";
}
}
}
cout << endl;
Instead of using your own custom methods, why not use a short and standard method?
Given an std::string input with the text, this will print the unique chars:
std::set<char> unique(input.begin(), input.end());
for (auto & c : unique)
{
std::cout << c << " ";
}
std::cout << std::endl;
You can use std::count and std::set:
#include <string>
#include <set>
#include <iostream>
using namespace std;
int main()
{
string s = "hellohowareyou";
set<char>the_set(s.begin(), s.end());
for (char i:the_set)
if (count(s.begin(), s.end(), i) > 1)
cout << i << endl;
}
Output:
e
h
l
o
If you are not allowed to use a map (and probably also not allowed to use a set), you could simply use an array of integers to count occurrences, with one entry for each possible char value. Note that a character - when taken as an ASCII value - can be directly used as an index for an array; however, to avoid negative indices, each character value should first be converted to an unsigned value.
#include <iostream>
#include <limits>
int main() {
const char* input = "aaaddbss";
int occurrences[UCHAR_MAX+1] = { 0 };
for (int i = 0;input[i] !='\0'; i++)
{
unsigned char c = input[i];
if (occurrences[c]==0) {
occurrences[c]++;
}
else if (occurrences[c]==1) {
occurrences[c]++;
cout << "duplicate: " << c << endl;
}
}cout << endl;
}

Count number of occurrences in string c++?

I'm used to java, and struggle with basic syntax of C++ despite knowing theory. I have a function that is trying to count the number of occurrences in a string, but the output is a tab bit weird.
Here is my code:
#include <cstdlib>
#include <iostream>
#include <cstring>
/*
main
* Start
* prompt user to enter string
* call function
* stop
*
* count
* start
* loop chars
* count charts
* print result
* stop
*/
using namespace std;
void count(const char s[], int counts[]);
int main(int argc, char** argv) {
int counts[26];
char s[80];
//Enter a string of characters
cout << "Enter a string: "; // i.e. any phrase
cin.getline(s,80);
cout << "You entered " << s << endl;
count(s,counts);
//display the results
for (int i = 0; i < 26; i++)
if (counts[i] > 0)
cout << (char)(i + 'a') << ": " << counts[i] << "Times " << endl;
return 0;
}
void count(const char s[], int counts[]){
for (int i = 0; i < strlen(s); i++)
{
char c = tolower(s[i]);
if (isalpha(c))
counts[c - 'a']++;
}
}
Here is the output:
Enter a string: Dylan
You entered Dylan
b: 1Times
c: 1Times
d: 2Times
f: 1Times
h: 1Times
i: 1229148993Times
j: 73Times
l: 2Times
n: 2Times
p: 1Times
r: 1Times
v: 1Times
Any help you can give me would be greatly appreciated. Even though this is simple stuff, I'm a java sucker. -_-
Your counts is uninitialized. You need to first set all of the elements to 0.
You need to zeros the counts vector.
Try
counts[26]={0};
I don't know about java, but you have to initialize your variables in C/C++. Here is your code working:
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
void count(const char s[], int counts[]){
for (int i = 0; i < strlen(s); i++)
{
char c = tolower(s[i]);
if (isalpha(c))
counts[c - 'a']++;
}
}
int main(int argc, char** argv) {
int counts[26];
char s[80];
//Enter a string of characters
cout << "Enter a string: "; // i.e. any phrase
cin.getline(s,80);
for(int i=0; i<26; i++)
counts[i]=0;
cout << "You entered " << s << endl;
count(s,counts);
//display the results
for (int i = 0; i < 26; i++)
if (counts[i] > 0)
cout << (char)(i + 'a') << ": " << counts[i] << "Times " << endl;
return 0;
}

ASCII Dec to Char in C++

I want to get every characters of ASCII in normal char. If I only put char key only, it would return dec.
My request:
char alph = //ascii dec to normal char
For example: A in dec is 65
Note: I don't have the characters, but I do have the ASCII codes in dec like 65.
because I need user input like 65
In this case you can do this:
#include <iostream>
using namespace std;
int main() {
int code;
cout << "Enter a char code:" << endl;
cin >> code;
char char_from_code = code;
cout << char_from_code << endl;
return 0;
}
This will ouput:
Enter a char code:
65
A
It seems you have misunderstood the concept.
The numerical value is always there. Whether you print it as the letter or the numerical value depends on how you print.
std::cout will print chars as letters (aka chars) so you'll need to cast it to another integer type to print the value.
char c = 'a';
cout << c << endl; // Prints a
cout << (uint32_t)c << endl; // Prints 97
cout << endl;
uint32_t i=98;
cout << i << endl;
cout << (char)i << endl;
Output:
a
97
98
b
This is the method, very simple and then just need to make your own user interface to get input dec
#include <iostream>
using namespace std;
int main() {
int dec = 65;
cout << char(dec);
cin.get();
return 0;
}
Looks like you need hex/unhex converter. See at boost, or use this bicycle:
vector<unsigned char> dec2bin( const string& _hex )
{
vector<unsigned char> ret;
if( _hex.size() < 2 )
{
return ret;
}
for( size_t i = 0; i <= _hex.size() - 2; i += 2 )
{
string two = string( _hex.data() + i, 2 );
stringstream ss( two );
string ttt = ss.str();
int tmp;
ss >> /*hex >>*/ tmp;
unsigned char c = (unsigned char)tmp;
ret.insert( ret.end(), c );
}
return ret;
}
int main()
{
string a = "65";
unsigned char c = dec2bin( a )[0];
cout << (char)c << endl;
return 0;
}

How to convert ASCII value into char in C++?

How do I convert 5 random ascii values into chars?
Prompt:
Randomly generate 5 ascii values from 97 to 122 (the ascii values for all of the alphabet). As you go, determine the letter that corresponds to each ascii value and output the word formed by the 5 letters.
My Code:
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main ()
{
srand (time(NULL));
int val1= rand()%122+97;
int val2= rand()%122+97;
int val3= rand()%122+97;
int val4= rand()%122+97;
int val5= rand()%122+97
cout<<val1<<" and "<<val2<<" and "<<val3<<" and "<<val4<<" and "<<val15<<". "<<
return 0;
}
To convert an int ASCII value to character you can also use:
int asciiValue = 65;
char character = char(asciiValue);
cout << character; // output: A
cout << char(90); // output: Z
for (int i = 0; i < 5; i++){
int asciiVal = rand()%26 + 97;
char asciiChar = asciiVal;
cout << asciiChar << " and ";
}
int main()
{
int v1, v2, v3, v4, v5,v6,v7;
cout << "Enter 7 vals ";
cin >> v1 >> v2 >> v3 >> v4 >> v5 >> v6 >> v7;
cout << "The phrase is "
<< char(v1)
<< char(v2) << " "
<< char(v3) << " "
<< char(v4)
<< char(v5)
<< char(v6)
<< char(v7);
system("pause>0");
}