Make a backward string its own string? - c++

I am brand new to programming so none of this may be right. I was just messing around trying to get the effect that I (finally) achieved. More for practice than anything else, but I wondered if there was another way to do it.
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1="Don't even think about it!";
string str2;
string str3;
string str4 = "";
for (int i = 0; i < str1.length(); i++){
str2 = str1.substr (0, str1.length()-i);
cout << str2 << str4;
for (int x = str2.length() - 1; x >= 0; x--){
str3 = str1[x];
cout << str3;
}
str4 = str4 + " ";
cout << "\n";
}
getch();
main();
return 0;
}
The question I have is this: Is there a way to make the str3, after it is backwards, its own string that I could then justify right instead of adding spaces with str4?

In order to get a reversed string, just pass the reverse-iterators to the constructor of a new string:
#include <iostream>
#include <string>
int main() {
std::string s = "this is a test";
std::string s_reversed(s.rbegin(), s.rend());
std::cout << s << "\n" << s_reversed << "\n";
}
The output is:
$ g++ test.cc && ./a.out
this is a test
tset a si siht
Applied to your problem:
#include <iostream>
#include <string>
int main() {
std::string s = "this is a test";
for (auto i = s.length(); i > 0; i--) {
std::cout << s;
std::cout << std::string(s.rbegin(), s.rend());
std::cout << '\n';
s[i-1] = ' ';
}
}
Note that there also is an algorithm in the standard library to reverse a container, e.g., a string: std::reverse().
For reference:
http://en.cppreference.com/w/cpp/string/basic_string/basic_string
http://en.cppreference.com/w/cpp/string/basic_string/rbegin
http://en.cppreference.com/w/cpp/string/basic_string/rend
http://en.cppreference.com/w/cpp/algorithm/reverse

You could try this:
replacing letters for whitespace and then reversing the string.
Concat the modified string with the reverse of it and print it.
std::string reverse(std::string str)
{
std::reverse(str.begin(), str.end());
return str;
}
int main()
{
std::string str = "Don't even think about it!";
size_t N = str.length();
for (size_t i = 1; i < N; i++)
{
std::cout << str << reverse(str) << std::endl;
str.replace(N-i,1, " ");
}
return 0;
}

First I would rename the variables so it is more clear what you are trying to achieve. Believe me, in two months you will not have a clue what this code actually does. Suggestion:
str1 -> baseString
str2 -> forwardsClippedString
str3 -> backwardsClippedString
str4 -> gapString
Maybe these names are not even good but I think they are better than str1, ..., str4.
Then I would make a separate method of the inner loop. After that make a separate method of the outer for loop - yes, main() shouldn't be complicated at all.
Finally instead of recursively calling main I suggest to do a while-loop. An infinite one if you please (but it would be nice if one key quits the loop then).

Related

How to concatenate strings in an output function?

Some languages have easy ways of doing this, but my question revolves in C and C++.
I wanna do something like this in Java:
public class sandbox {
public static void main(String[] args) {
System.out.println("Thank" + " you!");
}
}
And transfer it in C:
#include <stdio.h>
int main() {
/* The easiest way is like this:
char *text1 = "Thank";
char *text2 = " you";
printf("%s%s\n", text1, text2);
*/
printf("Thank" + " you."); // What I really want to do
}
How do I concatenate strings in a language like this?
You use just nothing:
puts ("Thank" " you.");
Concatenating strings is not that easy in C unfortunately, here's how to do it most succinctly:
char *text1 = "Thank";
char *text2 = " you";
char *text_concat = malloc(strlen(text1) + strlen(text2) + 1);
assert(text_concat);
text_concat = strcpy(text_concat, text1);
text_concat = strcat(text_concat, text2);
printf("%s\n", text_concat);
free(text_concat);
What I have understood from your question, hope the below solution will answer your question.
#include <stdio.h>
int main() {
char s1[100] = "Thank ", s2[] = "You";
int length, j;
// store length of s1 in the length variable
length = 0;
while (s1[length] != '\0') {
++length;
}
// concatenate s2 to s1
for (j = 0; s2[j] != '\0'; ++j, ++length) {
s1[length] = s2[j];
}
// terminating the s1 string
s1[length] = '\0';
printf("After concatenation: %s",s1);
return 0;
}
In C++, you can easily concatenate two string it by adding two string with a + operator.
#include <iostream>
using namespace std;
int main()
{
string s1, s2, result;
cout << "Enter string s1: ";
cin>>s1;
cout << "Enter string s2: ";
cin>>s2;
result = s1 + s2;
cout << "After concatenation: = "<< result;
return 0;
}
This is a concatenation, but is a constant or compile time concatenation, you can't concatenate strings like that, but in case you need to split a string constant in multiple parts is ok:
...
printf("Thank" " you."); // What I really want to do
...
For dynamic, runtime concatenation you need strcat like
strcat(text1, text2);
First you must assure that you have enough memory in target string, see this link http://www.cplusplus.com/reference/cstring/strcat/
Ok, that was the C way, but C++ has STL with std::string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "hello ", str2 = "world";
cout<< str1 + str2<< endl;
return 0;
}
It is not possible in C to do something like printf("Thank" + " you."); because C doesn't support Operator Overloading Unlike C++. You can refer Is it possible to overload operators in C?

Can I assign individual character to a string in c++?

I am trying to write a program to reverse a string. I used the following code but unfortunately it didn't work. I am a little confused why that happened.
Here is my code:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string InputString = "Hello";
string OutputString;
int length;
length = InputString.length();
for (int i=length-1, j=0; i >=0, j<length; i--, j++)
OutputString[j] = InputString[i];
cout << "The reverse string of " << InputString << " is "
<< OutputString << ".\n";
return 0;
}
My output is:
The reverse string of Hello is .
The problem isn't quite what you think it is. It is that OutputString is empty and any indexing into it will be out of bounds and lead to undefined behavior.
You might instead do something like
OutputString += InputString[i];
to append characters to the string.
Also, the loop condition i >=0, j<length will not work like you think either. You are using the comma expression so while both i >= 0 and j<length will be evaluated, only the result of j<length will be used. You probably want to use the logical and operator there: i >=0 && j<length.
I prefer to reverse strings like this:
#include <string>
#include <iostream>
int main(int argc,char** argv){
std::string hello = "hello";
for(std::size_t i=0;i < hello.length()/2; ++i)
{
std::swap(hello[i],hello[hello.length()-i-1]);
}
std::cout<<hello<<std::endl;
return 0;
}
Live Demo
Or you simply do:
string OutputString(InputString.rbegin(), InputString.rend());
cout << "The reverse string of " << InputString << " is "
<< OutputString << ".\n";

how to remove spaces from a string in C++

I used "using namespace std;" in my entire study in C++,so basically I don't understand something like std::out,please help me out.Let's say I have a code shown below,i want the two string to be the same when I compare them.
int main(void)
{
using namespace std;
char a[10] = "123 ";
char b[10] = "123";
if(strcmp(a,b)==0)
{cout << "same";}
return 0;
}
If you use std::string instead of char you could use the truncate functions from boost.
Use std::string to do it
std::string a("123 ");
std::string b("123");
a.erase(std::remove_if(a.begin(), a.end(), ::isspace), a.end());
if (a == b)
std::cout << "Same";
The difference made by using will be
using namespace std;
string a("123 ");
string b("123");
a.erase(remove_if(a.begin(), a.end(), ::isspace), a.end());
if (a == b)
cout << "Same";
It is generally advised not to use the using namespace std. Don't forget to include <string> and <algorithm>.
EDIT If you still want to do it the C way, use the function from this post
https://stackoverflow.com/a/1726321/2425366
void RemoveSpaces(char * source) {
char * i = source, * j = source;
while (*j != 0) {
*i = *j++;
if (*i != ' ') i++;
}
*i = 0;
}
use regex \\s+ to match all space characters and use regex_replace to remove it
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string text = "Quick brown fox";
std::regex spaces("\\s+");
// construct a string holding the results
std::string result = std::regex_replace(text, spaces, "");
std::cout << '\n' << text << '\n';
std::cout << '\n' << result << '\n';
}
reference: http://en.cppreference.com/w/cpp/regex/regex_replace

How to print an array of const chars?

I have written the following code to save in an char * array and print the following content:
band1.txt
band2.txt
...
band3.txt
The code seems right but what is printed on the console is very weird.
Code:
const char ** current_band = new const char * [103];
stringstream sstm;
string str;
for (i=0;i<103;i++){
current_band[i] = new char[11];
}
for (i=0;i<103;i++){
sstm.str("");
sstm << "band" << i+1 << ".txt";
str = sstm.str();
current_band[i] = str.c_str();
cout << current_band[i] << endl;
cout << i << endl;
}
for (i=0;i<103;i++){
cout << current_band[i] << endl;
cout << i << endl;
}
Console:
band1.txt
0
band2.txt
1
...
band103.txt
102
And then for the last loop:
band103.txt
0
band102.txt
1
band103.txt
2
band102.txt
3
...
band102.txt
101
band103.txt
102
How is this even possible?
EDIT: Actually i want the "bands" to be char* in order to call the ifstream current_band_file(current_band) constructor that wants such an argument
You have undefined behavior by using pointers to already destroyed objects.
Simply don't use raw pointers and raw arrays and such stuff yet.
std::string is your friend for strings, std::vector is your friend for arrays.
Example:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
auto main()
-> int
{
vector<string> band_names;
for( int i = 1; i <= 103; ++i )
{
band_names.push_back( "band" + to_string( i ) );
}
for( string const& name : band_names )
{
cout << name << endl;
}
}
As a minimal change to you existing code you can change:
current_band[i] = str.c_str();
to:
strcpy(current_band[i], str.c_str());
However, moving away from this mixed C and C++ to more idiomatic C++ (like Cheers and hth. - Alf's answer) will serve you better for the future.
Sticking with things like char[11] over std::string means you're stuck with:
The arbitrary choice of max length 11 even though probably there is no good technical reason for that limit.
Dealing with handling all the details of memory allocation which a proper C++ implementation hides.
The much less natural to read lower level code style.
As a band-aid you could replace:
current_band[i] = str.c_str();
with
if ( str.size() >= 11 )
throw std::runtime_error("string too long");
std::strcpy(current_band[i], str.c_str());
However it would be a much better idea to replace this whole thing with:
std::vector<std::string> current_band(103);
int i = 0;
for (auto &s : current_band)
{
// your sstm stuff, storing to s
}
Here's an alternative way that's a little more robust, readable and more likely to be correct.
#include <vector>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
vector<string> bands;
bands.reserve(103);
for(size_t i = 1 ; i <= 103 ; ++i) {
ostringstream ss;
ss << "band" << i;
bands.emplace_back( ss.str() );
}
for (size_t index = 0 ; index < bands.size() ; ++index) {
cout << index << " : " << bands[index] << endl;
}
return 0;
}
output:
Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
Executing the program....
$demo
0 : band1
1 : band2
2 : band3
...
100 : band101
101 : band102
102 : band103

extracting last 2 words from a sequence of strings, space-separated

I have any sequence (or sentence) and i want to extract the last 2 strings.
For example,
sdfsdfds sdfs dfsd fgsd 3 dsfds should produce: 3 dsfds
sdfsd (dfgdg)gfdg fg 6 gg should produce: 6 gg
You can use std::string::find_last_of function to find spaces.
int main()
{
std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds";
size_t found1 = test.find_last_of( " " );
if ( found1 != string::npos ) {
size_t found2 = test.find_last_of( " ", found1-1 );
if ( found2 != string::npos )
std::cout << test.substr(found2+1, found1-found2-1) << std::endl;
std::cout << test.substr(found1+1) << std::endl;
}
return 0;
}
The following will work if your strings are whitespace separated.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
string str = "jfdf fhfeif shfowejef dhfojfe";
stringstream sstr(str);
vector<string> vstr;
while(sstr >> str)
{
vstr.push_back(str);
}
if (vstr.size() >= 2)
cout << vstr[vstr.size()-2] << ' ';
if (vstr.size())
cout << vstr[vstr.size()-1] << endl;
return 0;
}
Returns the strings in the wrong order, but if that doesn't matter,
std::string s ("some words here");
std::string::size_type j;
for(int i=0; i<2; ++i) {
if((j = s.find_last_of(' ')) == std::string::npos) {
// there aren't two strings, throw, return, or do something else
return 0;
}
std::cout << s.c_str()+j+1;
s = " " + s.substr(0,j);
}
Alternatively,
struct extract_two_words {
friend std::istream& operator>> (std::istream& in , extract_two_words& etw);
std::string word1;
std::string word2;
};
std::istream& operator>> (std::istream& in , extract_two_words& etw) {
std::string str1, str2;
while(in) {
in >> str1;
in >> str2;
}
etw.word2 = str1;
etw.word1 = str2;
}
I would encourage you to have a look at the Boost library. It has algorithms and data structures that help you tremendously. Here's how to solve your problem using Boost.StringAlgo:
#include <boost/algorithm/string/split.hpp>
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds";
std::vector<std::string> v;
boost::algorithm::split(v, test, [](char c) { return c==' ';});
std::cout << "Second to last: " << v.at(v.size()-2) << std::endl;
std::cout << "Last: " << v.at(v.size()-1) << std::endl;
}
I would also encourage you to always use the vector::at method instead of []. This will give you proper error handling.
int main()
{
std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds";
size_t pos = test.length();
for (int i=0; i < 2; i++)
pos = test.find_last_of(" ", pos-1);
std::cout << test.substr(pos+1) << std::endl;
}
Simpler :)