0
Array operations in c++
how can we double each element inside an array in c++. We must have 2 arrays, the first must contain the letters to be doubled and the second will be an empty array which will receive each letter of the first array twice --> a becomes aa, b becomes bb etc... I'm new to c++ so operations with array I don't know. Here is the code where I doubled each elements without array https://code.sololearn.com/cI5ofFsTwGyQ/?ref=app
3 Respuestas
+ 1
Take first array as character array like :
char a[] = "abcd";
2nd take it as string array string aa[]={};
Then add aa[i]+=a[i]+a[i]; then print array.
hope it helps.....
edit:
need character to convert to string first this way..
0
Jayakrishna🇮🇳 I tried then I got an error --> https://code.sololearn.com/cX1dS0K5AQej/?ref=app
0
#include <iostream>
using namespace std;
int main() {
char a[] = "abc";
string aa[3] = {}; //array size needed
for (int i=0; i < 3; i++){
aa[i] += a[i];
aa[i] += a[i]; //two times add
cout << aa[i] << endl;
}
return 0;
}
edit:
Noël Julmiste Fils
//you can do this by string array also and here is having single array :
#include <iostream>
using namespace std;
int main() {
string a[] = {"a","b","c"};
for (int i=0; i < 3; i++){
a[i] = a[i]+a[i];
cout << a[i] << endl;
}
return 0;
}