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

18th Apr 2022, 9:23 AM
Julmiste Fils Noel
Julmiste Fils Noel - avatar
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..
18th Apr 2022, 9:31 AM
Jayakrishna 🇮🇳
18th Apr 2022, 9:53 AM
Julmiste Fils Noel
Julmiste Fils Noel - avatar
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; }
18th Apr 2022, 10:53 AM
Jayakrishna 🇮🇳