andre andre - 10 days ago 7
C++ Question

String concatenation does not work

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");
}

Answer

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

result += s[i];