Slicing string character correctly in C++ - c++

I'd like to count number 1 in my input, for example,111 (1+1+1) must return 3and
101must return 2 (1+1)
To achieve this,I developed sample code as follows.
#include <iostream>
using namespace std;
int main(){
string S;
cout<<"input number";
cin>>S;
cout<<"S[0]:"<<S[0]<<endl;
cout<<"S[1]:"<<S[1]<<endl;
cout<<"S[2]:"<<S[2]<<endl;
int T = (int) (S[0]+S[1]+S[2]);
cout<<"T:"<<T<<endl;
return 0;
}
But when I execute this code I input 111 for example and my expected return is 3 but it returned 147.
[ec2-user#ip-10-0-1-187 atcoder]$ ./a.out
input number
111
S[0]:1
S[1]:1
S[2]:1
T:147
What is the wrong point of that ? I am totally novice, so that if someone has opinion,please let me know. Thanks

It's because S[0] is a char. You are adding the character values of these digits, rather than the numerical value. In ASCII, numerical digits start at value 48. In other words, each of your 3 values are exactly 48 too big.
So instead of doing 1+1+1, you're doing 49+49+49.
The simplest way to convert from character value to digit is to subtract 48, which is the value of 0.
e.g, S[0] - '0'.

Since your goal is to count the occurrences of a character, it makes no sense to sum the characters together. I recommend this:
std::cout << std::ranges::count(S, '1');
To explain the output that you get, characters are integers whose values represent various symbols (and non-printable control characters). The value that represents the symbol '1' is not 1. '1'+'1'+'1' is not '3'.

Related

How getchar() function output

I want to understand how is getchar() function is working here?
I read getchar() returns the next character from stdin, or EOF if the end of file is reached.
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int decimal;
while(!isdigit(decimal=getchar()));
cout<<decimal;
}
I give input 25. It outputs 50. I don't understand why?
How is it giving 50.
getchar() reads a single character from the input stream and returns it's value. In your case, that is the character '2'. Most implementations (including yours it seems) use ASCII encoding where the character '2' has the value 50. The value assigned to decimal is therefore 50. Since decimal is an int, std::cout interprets it as a numeric value and prints it accordingly.
decimal is storing the first digit character it finds, which happens to be '2'. You're storing the value to an int, so cout outputs the ordinal value of decimal. The ASCII ordinal value of '2' is 50. You never even reached the 5 you entered.
Simple fix to make it display the character, not the ordinal value, would be to change the output code to:
cout << (char)decimal;
When you enter 25 it reads first character from this input. And first character is 2 here. ASCII value of 2 is 50. That's why you get 50 at the output.
If you want to see 2 at the output use like this
cout << (char) decimal << endl;
Here type-casting 50 into character. That is 2.
The C library function int getchar(void) gets a character (an unsigned char) from stdin.
Moreover, decimal is an integer type and isdigit(decimal) will check the character at ASCII decimal position.
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int decimal;
while(!isdigit(decimal=getchar()));\\when you input 25. It first gets 2.
\\ 2 gets stored as 50 inside decimal
\\ isdigit() is called which returns true for 50 which is ASCII of 2 and while breaks
cout<<decimal; \\ 50 is printed here. Type cast it to print 2.
}

How can i make a number like 123 into a string with ascii in C++?

I want to make a number like 77 into a string but I can't use ascii because it's only from 0-9. Is there some way to make numbers become a string? So this is the result at the end: You input a number and it outputs the number but as a string. Example: input:123; output:"123".
Each digit can use ASCII. The number 123 uses a 1, a 2, and a 3, and the string that represents that value uses the characters '1', '2', and '3'.
The way to do the conversion yourself is to get each digit by itself and add the digit to '0'. Like this:
int value = 123
std::string result;
while (value != 0) {
int digit = value % 10;
char digit_as_character = digit + '0';
result.insert(0, 1, digit_as_character);
value = value / 10;
}
This is pretty much what you'd do if you were doing it by hand:
start with the value get the last digit of the value by dividing by
10 and looking at the remainder
write down a digit for the remainder
divide the value by 10 to remove the last digit, since you don't need
it any more.
Who said anything about ascii? The C++ standard doesn't.
Use the platform-independent std::to_string(77) instead.
Reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string

Casting an int to a char. Not storing the correct value

I'm trying to store a number as a character in a char vector named code
code->at(i) = static_cast<char>(distribution(generator));
However it is not storing the way I think it should
for some shouldn't '\x4' be the ascii value for 4? if not how do I achieve that result?
Here's another vector who's values were entered correctly.
You are casting without actually converting the int to a char. You need:
code->at(i) = distribution(generator) + '0';
No. \xN does not give you the ASCII code for the character N.
\xN is the ASCII character† whose code is N (in hexadecimal form).
So, when you write '\x4', you get the [unprintable] character with the ASCII code 4. Upon conversion to an integer, this value is still 4.
If you wanted the ASCII character that looks like 4, you'd write '\x34' because 34 is 4's ASCII code. You could also get there using some magic, based on numbers in ASCII being contiguous and starting from '0':
code->at(i) = '0' + distribution(generator);
† Ish.

what does that mean, C programm for RLE

I am new to C so I do not understand what is happening in this line:
out[counter++] = recurring_count + '0';
What does +'0' mean?
Additionally, can you please help me by writing comments for most of the code? I don't understand it well, so I hope you can help me. Thank you.
#include "stdafx.h"
#include "stdafx.h"
#include<iostream>
void encode(char mass[], char* out, int size)
{
int counter = 0;
int recurring_count = 0;
for (int i = 0; i < size - 1; i++)
{
if (mass[i] != mass[i + 1])
{
recurring_count++;
out[counter++] = mass[i];
out[counter++] = recurring_count + '0';
recurring_count = 0;
}
else
{
recurring_count++;
}
}
}
int main()
{
char data[] = "yyyyyyttttt";
int size = sizeof(data) / sizeof(data[0]);
char * out = new char[size + 1]();
encode(data, out, size);
std::cout << out;
delete[] out;
std::cin.get();
return 0;
}
It adds the character encoding value of '0' to the value in recurring_count. If we assume ASCII encoded characters, that means adding 48.
This is common practice for making a "readable" digit from a integer value in the range 0..9 - in other words, convert a single digit number to an actual digit representation in a character form. And as long as all digits are "in sequence" (only digits between 0 and 9), it works for any encoding, not just ASCII - so a computer using EBCDIC encoding would still have the same effect.
recurring_count + '0' is a simple way of converting the int recurring_count value into an ascii character.
As you can see over on wikipedia the ascii character code of 0 is 48. Adding the value to that takes you to the corresponding character code for that value.
You see, computers may not really know about letters, digits, symbols; like the letter a, or the digit 1, or the symbol ?. All they know is zeroes and ones. True or not. To exist or not.
Here's one bit: 1
Here's another one: 0
These two are only things that a bit can be, existence or absence.
Yet computers can know about, say, 5. How? Well, 5 is 5 only in base 10; in base 4, it would be a 11, and in base 2, it would be 101. You don't have to know about the base 4, but let's examine the base 2 one, to make sure you know about that:
How would you represent 0 if you had only 0s and 1s? 0, right? You probably would also represent the 1 as 1. Then for 2? Well, you'd write 2 if you could, but you can't... So you write 10 instead.
This is exactly analogous to what you do while advancing from 9 to 10 in base 10. You cannot write 10 inside a single digit, so you rather reset the last digit to zero, and increase the next digit by one. Same thing while advancing from 19 to 20, you attempt to increase 9 by one, but you can't, because there is no single digit representation of 10 in base 10, so you rather reset that digit, and increase the next digit.
This is how you represent numbers with just 0s and 1s.
Now that you have numbers, how would you represent letters and symbols and character-digits, like the 4 and 3 inside the silly string L4M3 for example? You could map them; map them so, for example, that the number 1 would from then on represent the character A, and then 2 would represent B.
Of course, it would be a little problematic; because when you do that the number 1 would represent both the number 1 and the character A. This is exactly the reason why if you write...
printf( "%d %c", 65, 65 );
You will have the output "65 A", provided that the environment you're on is using ASCII encoding, because in ASCII 65 has been mapped to represent A when interpreted as a character. A full list can be found over there.
In short
'A' with single quotes around delivers the message that, "Hey, this A over here is to receive whatever the representative integer value of A is", and in most environments it will just be 65. Same for '0', which evaluates to 48 with ASCII encoding.

Converting an integer to to ascii values C++

I am attempting to turn a number into letters using ascii, at the moment I can do it one letter at a time:
EDIT: The output of an RSA encryption that I've been working on is currently in the form of an integer, I'm trying to work out how to convert it to the word/sentence which was the original input. I've nearly finished but I'm completely stuck at the last "hurdle". I'm adding context due to a comment asking why I would want to do this (or words to that effect).
EDIT: If during the encryption process I used the ASCII value - 87, all letters would be 2 digits long, eliminating the problem of some ASCII characters being 3 letters and some being 2, does this make the problem more approachable? (it limits me to only letter but that's fine for its purpose)
#include <string>
#include <iostream>
char returnChar(int x)
{
return (char) x;
}
int main()
{
std::cout << returnChar (119);
}
This converts 32 --> w.
How could I adapt this function to allow me to change "3232" --> "ww" or any other integer to ascii characters, e.g. "32242713" --> "word".
EDIT: I think using some kind of mod function to split it into chunks of two numbers which could then be converted to characters might work?
How do I overcome the problem of some ascii characters having 2 digits and some having 3 digits? I think this problem has been solved as described in the second edit
If you can see that I've approached this in entirely the wrong way, could you suggest a viable alternative approach for me to try please?
Thanks for any feedback.
What you're asking for is not possible. You have a few alternatives:
Change the int to a string and put white spaces/other characters inside the string:
std::string test = "119 119";
Convert the total value to binary, and parse byte by byte:
unsigned int test = 30583; // 119*256+119
char a = (test>>8)&0xff;
char b = test&0xff;
Pass the data in a vector and convert one element at a time:
std::vector<char> returnChar(const std::vector<int> &data){
std::vector<char> output;
for(unsigned int i=0;i<data.size();i++)
output.push_back(char(data[i]));
return output;
}
I would probably stick with the second method, since - a wild guess here - it shouldn't change much things inside where you actually generate the numbers.