String concatenation does not work - c++

I'm trying to concatenate two strings and the result string is not concatenated at all. I don't know where is the problem. The line that is a problem is that line
tempString = tempString+ result;
Here is the full code
#include <iostream>
#include <cstdio>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <limits>
#include <functional>
#include <algorithm>
#include <cmath>
#include <string>
#include <ostream>
#include <sstream>
#include <bitset>
#include <numeric>
#include <fstream>
#include <stdint.h>
using namespace std;
#define OTHER 3
#define SQUARE 1
#define RECTANGLE 2
static std::string accum(const std::string &s)
{
string result = "";
string tempString = "";
for (size_t i = 0; i < s.length(); i++)
{
int temp = i+1;
if (i == 0)
{
result[i] += s[i];
result[i] = toupper(result[i]);
tempString = tempString+ result;
tempString += "-";
}
else
{
if (i!=1)
{
tempString += '-';
}
while (temp > 0)
{
tempString+=s[i];
temp--;
}
}
}
for (int i = 0; i < tempString.size(); i++)
{
if (tempString[i] == '-')
{
tempString[i + 1] = toupper(tempString[i + 1]);
}
}
return tempString;
}
int main() {
string result = accum("abcd");
}

Change this line:
result[i] += s[i];
to this:
result += s[i];

Related

why have i bus error 10, when I put cout<<""<<endl; out of my code

I have written a code, but there’s one Problem. When I put cout<<""<<endl; out, I have Bus error: 10. How can I solve this Problem.
This is my code:
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstdlib>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <assert.h>
#include <ctime>
#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;
char* rotate(char const* a, int b)
{
char* r;
for (int i = 0; i < int(strlen(a)); i++) {
if (i >= int(strlen(a)) - b) {
r[i] = a[i - int(int(strlen(a)) - b)];
}
else if (i + b < 0) {
r[i] = a[int(strlen(a) + b)];
}
else {
r[i] = a[i + b];
}
}
r[int(strlen(a))] = '\0';
return r;
}
char* Crypt(char* Input, bool Encrypt, int InitialRotation, int Rotation)
{
char const* Table;
try {
Table = new char[size_t(strlen(Table))];
}
catch (...) {
throw "No Memory";
};
char* a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
try {
a = new char[size_t(strlen(a))];
}
catch (...) {
throw "No Memory";
};
char* ActualTable;
try {
ActualTable = new char[size_t(strlen(ActualTable))];
}
catch (...) {
throw "No Memory";
};
int Offset = InitialRotation;
char* Result;
try {
Result = new char[size_t(strlen(Result))];
}
catch (...) {
throw "No Memory";
};
int b;
Table = "JPGVOUMFYQBENHZRDKASXLICTW";
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < int(strlen(Input)); i++) {
for (int c = 0; i < int(strlen(a)); c++) {
if (a[c] == Input[i]) {
b = c;
break;
}
}
cout << "" << endl;
ActualTable = rotate(Table, Offset);
Result[i] = rotate(Table, Offset)[b];
Offset = Offset + Rotation;
}
return Result;
}
int main()
{
char* A = "ZZZZ";
cout << Crypt(A, 1, 1, 4) << endl;
return 0;
}

Having a issue while iterating all files and folders recursively in C++

I have the following code to iterate all the files and folders on all the drives on a system. Can you help me please understand what is missing ? I'm getting an exception for fs::recursive_directory_iterator. Thanks !
#include <iostream>
#include <Windows.h>
#include <vector>
#include <string>
#include <filesystem>
namespace fs = std::filesystem;
std::vector<std::string> getDrive() {
std::vector<std::string> Drives;
char* lDrives = new char[MAX_PATH]();
if (GetLogicalDriveStringsA(MAX_PATH, lDrives))
{
for (int i = 0; i < 100; i += 4)
{
if (lDrives[i] != (char)0)
{
Drives.push_back(std::string{ lDrives[i],lDrives[i + 1],lDrives[i + 2] });
}
}
}
delete[] lDrives;
return Drives;
}
int main()
{
std::vector<std::string> diskdrives = getDrive();
for (auto drv : diskdrives) {
for (auto& file : fs::recursive_directory_iterator(drv)) {
try {
if (fs::is_directory(file))
{
}
else
{
std::cout << file.path().string() << std::endl;
}
}
catch (const std::exception&)
{
}
}
}
}

insert string into vector character by character

I'm trying to insert the characters of the string into a char vector but place the letters in reverse order . can anyone tell me why this doesn't work
int main()
{
string a = "Hello";
vector<char> arr(5);
for(int i = 4 ; i == 0 ; i--)
{
arr.push_back(a[i]);
cout << arr[i];
}
return 0;
}
im trying to push back the character in reverse order 1 by 1
Their are several problems with your code:
you are creating a vector whose size is initially 5, and then you are attempting to push 5 additional chars into it, for a total of 10 chars. You need to either:
initialize it's capacity instead of its size.
initialize the size as you are, but use arr[4-i] instead of arr.push_back() inside your loop.
your loop is never entered at all, since i == 0 is never true.
Try something more like this instead:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string a = "Hello";
size_t len = a.size();
vector<char> arr;
arr.reserve(len);
for(int i = len-1; i >= 0; i--) {
arr.push_back(a[i]);
}
for(size_t i = 0; i < len; ++i) {
cout << arr[i];
}
return 0;
}
Alternatively:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string a = "Hello";
size_t len = a.size();
vector<char> arr(len);
for(int i = len-1; i >= 0; i--) {
arr[len-1-i] = a[i];
}
for(size_t i = 0; i < len; ++i) {
cout << arr[i];
}
return 0;
}
Another way to deal with this in a more C++-ish way is to use iterators instead:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string a = "Hello";
vector<char> arr;
arr.reserve(a.size());
for(auto iter = a.rbegin(); iter != a.rend(); ++iter) {
arr.push_back(*iter);
}
for(auto ch : arr) {
cout << ch;
}
return 0;
}
Alternatively:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string a = "Hello";
vector<char> arr(a.size());
auto arr_iter = arr.begin();
for(auto a_iter = a.rbegin(); a_iter != a.rend(); ++a_iter, ++arr_iter) {
*arr_iter = *a_iter;
}
for(auto ch : arr) {
cout << ch;
}
return 0;
}
And then you can get rid of the manual loops altogether:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
int main() {
string a = "Hello";
vector<char> arr;
arr.reserve(a.size());
copy(a.rbegin(), a.rend(), back_inserter(arr));
cout.write(arr.data(), arr.size());
return 0;
}
Alternatively:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
string a = "Hello";
vector<char> arr(a.size());
copy(a.rbegin(), a.rend(), arr.begin());
cout.write(arr.data(), arr.size());
return 0;
}
Alternatively:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string a = "Hello";
vector<char> arr(a.rbegin(), a.rend());
cout.write(arr.data(), arr.size());
return 0;
}
I'm not sure why you're complicating matters when a vector is perfectly capable of taking an iterator in the constructor? The vast majority of your code can therefore be replaced with a simple:
vector<char> arr(a.rbegin(), a.rend());
The complete program below shows this in action:
#include <iostream>
#include <string>
#include <vector>
using std::cout; using std::string; using std::vector;
int main() {
string a = ")-: yug emosdnah a si xaP";
vector<char> arr(a.rbegin(), a.rend());
for (auto ch: arr) cout << ch;
cout << '\n';
}
for(int i = 4 ; i == 0 ; i--)
That means to keep going while i is equal to zero - but i starts out at four, so the for loop terminates immediately.
You probably meant
for(int i = 4 ; i >= 0 ; i--)

How to access numbers in string and convert it to integer?

I am using stoi function here and it is giving invalid argument error...
Here, the input file is something like "S13S12S11S10S1". I want to save the numbers in an array rank like rank[0]=13 rank[1]=12 and so on...
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
ifstream fin("input.txt");
string input;
fin>>input;
int count=0;
int val;
int rank[4];
for(int i=0 ; i < input.size(); i++)
{
string s1,s2;
s1=input[i];
s2=input[i+1];
if(s1[0]!='S' && s1[0]!='H' &&s1[0]!='D' && s1[0]!='C')
{
int a=stoi(s1);
rank[count]=a;
if(s2[0]!='S' && s2[0]!='H' &&s2[0]!='D' &&s2[0]!='C')
{
int temp;
int b=stoi(s2);
rank[count]=10+b;
count++;
i++;
}
else{
count++;
}
}
}
for (int count=0; count<=4 ; count++)
{
cout<<rank[count];
cout<<"\n";
}
}
You can tokenize the input string, using 'SHDC' for delimiters. And then use atoi to convert the tokens to integers. I would use a vector to store your rank values, if your input file(s) could have a varying number of tokens.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
ifstream fin("input.txt");
string input;
fin >> input;
const char *delimiters = "SHDC";
char *next_token = NULL;
char *token = strtok_s(const_cast<char*>(input.c_str()), delimiters, &next_token);
vector<int> values;
while (token != NULL) {
values.push_back(atoi(token));
token = strtok_s(NULL, delimiters, &next_token);
}
for (int i = 0; i < values.size(); ++i) {
cout << values[i] << endl;
}
}

c++ formating error with cstrings when using tolower

Hi so I am trying to take a cstring and make it lowercase, but when I am printing the cstring at the end I am getting a weird format box where some of the letters should be. Do anyone have any ideas?
#include <string>
#include <iostream>
#include <string.h>
using namespace std;
int main ()
{
int i=0;
char* str="TEST";
char c;
char* cstr = new char[strlen(str) + 1];
while (str[i])
{
c = str[i];
c = tolower(c);
strcat(cstr, &c);
i++;
}
cout << cstr << endl;
return 0;
}
The problem is that you are calling strcat incorrectly. The second parameter is not a null-terminated string.
You really don't need to call strcat at all. Just write directly to the output string:
Try:
while (str[i])
{
c = str[i];
c = tolower(c);
cstr[i] = c;
i++;
}
cstr[i] = 0;
or, equivalently:
while(str[i])
{
cstr[i] = tolower(str[i]);
i++;
}
cstr[i] = 0;
strcat expects a null-terminated char*, so by giving the address of a local char you are invoking undefined behavior.
Additionally, new char[std::strlen(str) + 1] does not initialize the array to 0s, meaning cstr won't be properly null-terminated either; adding () to the new[] causes the array to be value-initialized.
Try this instead:
#include <cstddef>
#include <cctype>
#include <cstring>
#include <ostream>
#include <iostream>
int main()
{
char const* str = "TEST";
char c[2] = { };
char* cstr = new char[std::strlen(str) + 1]();
std::size_t i = 0;
while (str[i])
{
c[0] = static_cast<char>(std::tolower(str[i++]));
std::strcat(cstr, c);
}
std::cout << cstr << std::endl;
delete [] cstr;
}
The second argument of strcat is supposed to be a null terminated string, not the address of a single character. strcat isn't appropriate for this use.
int main ()
{
const char* str="TEST";
char* cstr = new char[strlen(str) + 1];
cstr[strlen(str)] = 0;
for (int i = 0; str[i]; ++i) {
cstr[i] = tolower(str[i]);
}
cout << cstr << endl;
}
#include <cstddef>
#include <cctype>
#include <cstring>
#include <ostream>
#include <iostream>
#include <string>
int main()
{
std::string str = "TEST";
std::string cstr;
for (std::string::const_iterator it = str.begin(); it!= str.end(); ++it)
cstr.push_back(tolower(*it));
std::cout << cstr << std::endl;
}
Or even shorter:
#include <algorithm>
#include <iterator>
...
std::transform(str.begin(), str.end(), std::back_inserter(cstr), tolower);