Code hangs for string input greater than 1000 characters
All characters in P & Q are lower-case English letters.
#include<iostream>
#include<vector>
using namespace std;
int main(){
// 1st example
string p,q;
cin >> p >> q;
// 2nd example
char p[1500],q[1500];
scanf("%s",p);
cin >> q ;
return 0;
}
Both run fine for strings less than 1000 characters.
Both alternative examples break for strings greater than 1000 characters.
I just need a way to input strings of max 1500 chracters
Edit:Turns out XCode was at fault. It works fine on the server
If you pass Unicode input to this, the buffers will overflow & you will crash.
Edited to add:
If you're lucky. Note that lowercase English Unicode characters are still two bytes each. If your input is from a file, you cannot tell by looking whether it's Unicode, you need to open it in a hex editor to be sure.
To test this, make the buffers twice as large as the input (p[2001], q[2001]).
But using static buffers is the wrong way to do this, string is the right way: the upper limit is something like 2^32 characters.
Related
Hello I am trying to do problem word capitalization on code forces and to do the problem i am trying to use ascii table(my professor said I cant use cmath and anything other than loops arrays ascii table and the basics) it prints the number of the capital letter on the ascii lets say the ascii code for small a is 96 and capital A is 100 it prints 100 not A here's my code.
#include <iostream>
using namespace std;
int main()
{
string s;
cin>>s;
char x=s[0];
cout<<x+22<<s;
}
Because 22 is an int, the type of x+22 is also int. You need to convert it back into a char for iostream to interpret it as a character:
cout << char(x+22) << s;
Note that I only addressed what you asked about: A number being printed instead of a character. There might be other small errors in there for you to find.
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'.
First of all, i would like to read from plain text, i read hundreds of webpages about it and i just can't make it. I want to read every byte of the file and every two byte is a number what i want to store.
I want to read: 10 20.
I get: ASCII code of 1, ASCII code of 0, ASCII code of space etc. etc.
I tried several things, like stream.get, or stream.read, tried to convert with atoi but then i can't concatenate the two digits, i tried sprintf but all of them failed.
Array of ASCII codes:
char ASCII[] = "10 20";
Convert to integer variables:
std::istringstream iss(ASCII);
int x,y;
iss >> x >> y;
Done.
Here's the working sample: http://ideone.com/y8ZRGs
If you want to do this with your own code, there are only two things you need to be able to do.
First, you need to convert from the ASCII code of a digit to the number it represents. This is as simple as subtracting '0'.
Second, you need to convert from the numerical value of each digit of a two digit number to the number that represents. This is simple -- if T is the tens place and U is the units, it's 10T + U.
So, for example:
int twoDigitNumber (char tens, char units)
{
return 10 * (tens - '0') + (units - '0');
}
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.
I have to write a function that will read input from a file. The file is set up: one character, space, word, space, throughout the file, like such:
A space 1 space 2 space... etc
I need to extract the whitespace following the one character and NOT the whitespace following the word.
How can I go about doing this? Should I just make it so the function writes the whitespace itself instead of extracting it?
Also, I am importing this info into a 2-d char array. Will I run into problems trying to write integers to a char array?
Something like this maybe?
#include <iostream>
#include <fstream>
int main() {
char myChar;
char theWS;
std::string word;
std::ifstream in("example.txt");
while(in >> myChar >> std::noskipws >> theWS >> word >> std::skipws) {
std::cout << myChar << theWS << word << '\n';
}
}
You should've been exposed to the idea of a tokenizer by now. This is the structure you need.
You will be fine writing integers into character arrays. Since C and C++ represent ascii characters as small numbers anyways, handling them is easy. Some examples of the number values which correspond to specific chars: '0' => 48, '1' => 49, ... , 'A' => 65, 'B' => 66, etc.
Take a look at http://www.asciitable.com/ for the full set of ascii characters and their corresponding values.
This also allows you to perform mathematical operations on characters such as 'A' + 1 => 'B'
as well as convert between numbers and characters (char) 65 => 'A'