Insert spaces into a string - c++

I have a string and I want to insert spaces between the digits.
Example:
Input String: 123456
Output String: 1 2 3 4 5 6

You could just call the string as an array
std::string str = "123456";
std::string new_string = "";
int string_length = 6;
for(int i=0; i<string_length; i++){
new_string += str[i];
if(i != string_length-1) { new_string += " "; }
}
There are more efficient ways to do it, but this illustrates the behavior in an easily understandable way, with little steps at a time.

With range-v3 you could do:
namespace rv = ranges::views;
auto res = s | rv::intersperse(' ') | ranges::to<std::string>;
Here's a demo.

Best way is to implement your own custom split function.
Have a look at the following implementation:
#include <iostream>
#include <string>
bool isDigit(char c){
return (c>='0' && c<='9');
}
std::string splitOnDigits(std::string s){
std::string out = "";
for(int i=0;i<s.length();i++){
if(isDigit(s[i-1]) || (!isdigit(s[i-1]) && isDigit(s[i]))){
out += " ";
}
out += s[i];
}
return out;
}
int main()
{
std::string s = "123456";
std::cout<<splitOnDigits(s)<<std::endl;
return 0;
}
Output:
1 2 3 4 5 6

Related

How do i change char to number?

i need to change some character into numbers for example:
I = 1
R = 2
E = 3
A = 4
S = 5
G = 6
T = 7
B = 8
P = 9
O = 0
input example: HELLO IM GOOD
output example: H3LL0 1M G00D
Are you trying to make us do your homework?
Anyhow, there are multiple possibilities.
For the starter student - The most basic one is looping through your string and replacing each needed char with a new one (you can use a switch case, look-up-tables, etc).
You can convert to a string and use it's methods as such:
string s;
s="HELLO IM GOOD"
s.replace('I,'1')
s.replace('R,'2')
.
.
.
cout << s; //print solution
This code helps:)
#include <iostream>
#include<stdio.h>
using namespace std;
int main() {
string s;
getline (cin, s); //used to get string input with spaces
string s1 = "OIREASGTBP";
string s2 = "0123456789";
for (int i=0; i<s.size(); i++)
{
int a = s1.find(s[i]);
if(a != -1)
{
s[i] = s2[a];
}
}
cout<<s;
}

Writing program in C++ using Microsoft VS, but I get a debug assertion message here. It runs on cpp.sh and repl.it fine, but not on VS. What can I do?

This function is meant to remove all special characters, numbers, and whitespace from the char array.
// Michael E. Torres II
// Vigenere Cipher
// February 4, 2018
// C++ code to implement Vigenere Cipher
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <iterator>
#include <sstream>
#include <functional>
using namespace std;
// This function generates the key in
// a cyclic manner until it's length isi'nt
// equal to the length of original text
string generateKey(string str, string key)
{
int x = str.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}
// This function returns the encrypted text
// generated with the help of the key
string cipherText(string str, string key)
{
string cipher_text;
for (int i = 0; i < str.size(); i++)
{
// converting in range 0-25
int x = (str[i] + key[i]) % 26;
// convert into alphabets(ASCII)
x += 'A';
cipher_text.push_back(x);
}
return cipher_text;
}
// This function decrypts the encrypted text
// and returns the original text
string originalText(string cipher_text, string key)
{
string orig_text;
for (int i = 0; i < cipher_text.size(); i++)
{
// converting in range 0-25
int x = (cipher_text[i] - key[i] + 26) % 26;
// convert into alphabets(ASCII)
x += 'A';
orig_text.push_back(x);
transform(orig_text.begin(), orig_text.end(), orig_text.begin(), ::tolower);
}
return orig_text;
}
string removeNonAlpha(char *str)
{
unsigned long i = 0;
unsigned long j = 0;
char c;
while ((c = str[i++]) != '\0')
{
if (isalpha(c)) // this is where the breakpoint is automatically placed
{
str[j++] = c;
}
}
str[j] = '\0';
return str;
}
// Driver program to test the above function
int main(int argc, char *argv[])
{
string keyword = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
stringstream ss;
char a[] = "“I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right.” – Albert Einstein “Imagination is more important than knowledge. For knowledge is limited, whereas imagination embraces the entire world, stimulating progress, giving birth to evolution.” – Albert Einstein";
int i = 0;
string str = removeNonAlpha(a);
str.append(512 - str.length(), 'X');
transform(str.begin(), str.end(), str.begin(), ::toupper);
transform(keyword.begin(), keyword.end(), keyword.begin(), ::toupper);
string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);
transform(cipher_text.begin(), cipher_text.end(), cipher_text.begin(), ::tolower);
transform(key.begin(), key.end(), key.begin(), ::tolower);
string orig = originalText(cipher_text, key);
cout << "Original/Decrypted Text : " << "\n";
for (int i = 0; i < orig.size(); i += 81)
orig.insert(i, "\n");
cout << orig;
cout << "\n\n" << "Ciphertext : " << "\n";
for (int i = 0; i < cipher_text.size(); i += 81)
cipher_text.insert(i, "\n");
cout << cipher_text;
cout << "\n\nPress ENTER key to Continue\n";
getchar();
return 0;
}
The char array works fine with this while loop, so long as there are no special characters [.,%$#!^]. As soon as there are any special characters in the char array, it gives me the debug assertion:
"Program: ...\Projects\ConsoleApplication17\Debug\ConsoleApplication17.exe
File: minkernel\crts\ucrt\src\appcrt\convert\isctype.cpp
Line: 42
Expression: c >= -1 && c <= 255
...
The program '[11048] ConsoleApplication17.exe' has exited with code 3 (0x3)."
If I run this on repl.it or cpp.sh, I get no issues though. I appreciate any help. Thank you.
It isn't done at all. It needs to be cleaned up a lot, but I'm just trying to test it as is.
see https://msdn.microsoft.com/en-us/library/xt82b8z8.aspx
isalpha expects a number between 0 and 0xFF:
The behavior of isalpha and _isalpha_l is undefined if c is not EOF or
in the range 0 through 0xFF, inclusive. When a debug CRT library is
used and c is not one of these values, the functions raise an
assertion.
You need to cast you char to an unsigned char before passing to isalpha.

C++: Reading lines of integers from cin

As I'm familiarizing myself with the I/O aspect of C++, I'm trying to write a program to read some lines of integers from std::cin. Say the input looks like this:
1 2 3
4 5 6
7 8 9
10 11 12
How can I read the above lines into a 2D vector?
vector<vector<int>> nums;
/*
... some code here and nums will look like the following:
nums = {
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12}
}
*/
I've also tried to read the above lines of integers to a 1D vector, but I'm having some issues dealing with the '\n' character. My code is:
string rawInput;
vector<int> temp;
while(getline(cin, rawInput, ' ') ){
int num = atoi( rawInput.c_str() );
temp.push_back(num);
}
And the final result I got by printing out all the elements in the "temp" vector is:
1 2 3 5 6 8 9 11 12 // 4, 7, 10 went missing
Any help is appreciated. Thank you.
First use getline to grab an entire line, then you can use a istringstream to create a stream of ints just for that line.
At that point it's just a matter of creating each subvector of ints using the vector constructor that takes two iterators. An istream_iterator<int> on your istringstream gets this done:
std::vector<std::vector<int>> nums;
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream ss(line);
nums.emplace_back(std::istream_iterator<int>{ss}, std::istream_iterator<int>{});
}
What is happening is since you are using only ' '(space) as deliminator, the input happens to be
1
2
3\n4 //<------ Newline also comes with the input
...
So, you are passing 3\n4, 6\n7 etc to atoi it returns 3,6 etc(atoi parses the input till first non-digit input) and the 4,7 is lost.
To achieve want you want you can use getline with istringstream (keeping the default deliminator as newline)
string rawInput;
vector<vector<int>> temp;
while(getline(cin, rawInput) ){
istringstream bufferInput(rawInput);
temp.push_back(vector<int>{std::istream_iterator<int>{bufferInput}, std::istream_iterator<int>{}});
}
you can use stringstream
string rawInput;
vector<int> temp;
stringstream ss;
while(getline(cin,rawInput)){
ss<<rawInput;
vector<int> temp;
int x;
while(ss>>x){
temp.push_back(x);
}
num.push_back(temp)
}
I recently wrote an answer to another question but with a few adaptations it achieves exactly what you are looking for (I hope):
#ifndef _IOSTREAM_H
#include <iostream>
#endif
#ifndef _STRING_H
#include <string>
#endif
#ifndef _VECTOR_H
#include <vector>
#endif
using namespace std;
enum XYZ { X = 0, Y = 1, Z = 2 };
struct Vector {
float x, y, z;
Vector(float _x=0, float _y=0, float _z=0) {
x = _x;
y = _y;
z = _z;
}
float& operator[](size_t index) {
if (index == XYZ::X) return x;
if (index == XYZ::Y) return y;
if (index == XYZ::Z) return z;
throw new exception;
}
};
#define min(a, b) (((a) < (b)) ? (a) : (b))
bool isCharNumeric(char c) {
const char* numbers = "0123456789";
for (size_t index = 0; index < strlen(numbers); index++)
if (c == numbers[index]) return true; return false;
}
vector<Vector> parseNumbers(string str_in) {
str_in += " "; //safe, no out of bounds
vector<Vector> results = {};
char currentChar;
char skipChar = ' ';
bool found_period = false;
size_t count_len = 0;
Vector vector_buffer(0,0,0);
XYZ current_axis = (XYZ)0;
for (size_t index = 0; index < str_in.length(); index++) {
currentChar = str_in[index];
if (currentChar == skipChar || currentChar == '\n' || currentChar == '\t')
continue;
else if (isCharNumeric(currentChar)) {
string word = ""; //word buffer
size_t word_len = min(min(str_in.find_first_of(' ', index + 1) - (index), str_in.find_first_of('\n', index + 1) - (index)), str_in.find_first_of('\t', index + 1) - (index)); //whatever char comes first; newline, tab or space
//append chars of following word checking if it is still valid number char
if (word_len > 0) {
size_t count_word_len = 0;
for (count_word_len = 0; count_word_len < word_len; count_word_len++)
if (isCharNumeric(str_in[index + count_word_len])) {
word += str_in[index + count_word_len];
}
else if (str_in[index + count_word_len] == '.' && isCharNumeric(str_in[index + count_word_len + 1])) {
//Floating-point numbers
word += '.';
found_period = true;
continue;
}
else {
word = "";
continue;
}
vector_buffer[current_axis] = stof(word);
if (current_axis == XYZ::Z) {
current_axis = XYZ::X;
results.push_back(vector_buffer);
}
else {
current_axis = (XYZ)(current_axis + 1);
}
index += count_word_len;
word = "";
continue;
}
}
}
return results;
}
Example implementation:
int main(int argc, char** argv) {
string user_input;
cin >> user_input;
vector<Vector> numbers = parseNumbers(user_input);
for each (Vector v in numbers) {
cout << "X=" << v.X << "\n";
cout << "Y=" << v.Y << "\n";
cout << "Z=" << v.Z << "\n\n";
}
}
Suprisingly none of the answers use the istream stream operator:
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
When stream is empty eofbit is set, so run a while loop on that.
Works great for all types, and can be overloaded for custom types (such as 2D texture).

Taking Multiple inputs in C++

I just began learning C++ and had a rookie question.
Suppose I am given an input separated by spaces ex 2 4 56 or 2 1 10 15 or hi bye ok.
How can I store the values in an array as the length of input is not know.
Read up on std::vector. It can grow to the size needed.
search for spaces and split the string in each space as the following
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> GetInputs(string s)
{
vector<size_t> foundSpacesPositions;
vector<string> results;
for (size_t i = 0; i < s.length(); i++)
{
if (isspace(s[i]))
{
foundSpacesPositions.push_back(i);
}
}
size_t start = 0;
for (size_t i = 0; i < foundSpacesPositions.size(); i++)
{
if (foundSpacesPositions[i] == start)
{
++start;
continue;
}
results.push_back(s.substr(start, foundSpacesPositions[i] - start));
start = foundSpacesPositions[i] + 1;
}
if (start < s.length() - 1)
results.push_back(s.substr(start, s.length() - 1));
return results;
}
int _tmain(int argc, _TCHAR* argv[])
{
string s = "a dd 8 ll ehh fd $%^ & 89 . ";
vector<string> results = GetInputs(s);
for (auto& res : results)
{
cout << res << endl;
}
cin.get();
return 0;
}
/* OUTPUT
a
dd
8
ll
ehh
fd
$%^
&
89
.
*/
Well, if the input is given as a string, you can easily use a std::istringstream. If the input is given in stdin, you can just use the std::cin stream. std::istream's operator<< will take integers from a string using spaces as a default delimiter. Once you've read all the input on the stream, std::istream's operator bool will return false, and you can stop reading.
An example implementation would be:
vector<int> getIntsFromString(const string& s) {
istringstream ss(s);
int i;
vector<int> result;
while (ss >> i) result.push_back(i);
return move(result);
}
Suppose you are getting multiple lines with a variable number of integers from stdin, and you want to store them in vector<int>'s. You can do something similar to:
string buf;
while (getline(cin, buf)) {
auto v = getIntsFromString(buf);
for (auto i : v) cout << i << ' ';
cout << endl;
}

Fix/Improve Word Wrap Function

I have a simple word wrap function that takes a long string as an input and then breaks that string into smaller strings and adds them to an array to be outputted later. Right now the last word or two isn't outputting. That's the main problem. However, I would also like to improve the function. I know it's kind of messy. I was wondering if there are any better ways of solving this problem. I think the array is unnecessary but I don't know how else to do it. After the array is filled with all the smaller strings, I just output them to a text file. Any suggestions would be greatly appreciated.
Here's the Word Wrap function:
void WordWrap(string inputString, string formatedAr[], const int SIZE)
{
unsigned int index;
unsigned int word;
unsigned int max = 65;
string outWord;
string outLine;
outWord = "";
outLine = "";
word = 0;
for(int i = 0; i < SIZE; i++)
{
formatedAr[i] = "";
}
for(index = 0; index <= inputString.length(); index++)
{
if(inputString[index] != ' ')
{
outWord += inputString[index];
}
else
{
if(outLine.length() + outWord.length() > max)
{
formatedAr[word] = outLine;
word++;
outLine.clear();
}
outLine += outWord + " ";
outWord.clear();
}
}
formatedAr[word] = outLine;
}
And this is where I call the function and output the array:
WordWrap(dvdPtr -> synopsis, formatedAr, SIZE);
index = 0;
while(index < SIZE && formatedAr[index] != "")
{
outFile << formatedAr[index] << endl;
index++;
}
Here is an example code.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void WordWrap(const string& inputString, vector<string>& outputString, unsigned int lineLength)
{
istringstream iss(inputString);
string line;
do
{
string word;
iss >> word;
if (line.length() + word.length() > lineLength)
{
outputString.push_back(line);
line.clear();
}
line += word + " ";
}while (iss);
if (!line.empty())
{
outputString.push_back(line);
}
}
/*
A simple test:
Input string: "aaa bbb ccccccc dddd d111111111111111 33333 4444444444 222222222 ajdkjklad 341343"
Length per line: 20
Output lines of strings:
Line 1: aaa bbb ccccccc dddd
Line 2: d111111111111111
Line 3: 33333 4444444444
Line 4: 222222222 ajdkjklad
*/