Arrays

I need to prompt the user to input two separate 3 character strings and store them in arrays. Then I reserve order the strings and add them together using a third array.

"Write a program that will prompt the user for two strings. Your program should save these two strings in two different character arrays respectively. Create a third array which is used for merging these two arrays into one character array. The third array should contain all the characters from the first string + second string in a reverse order."

Example
string1= abc
string2= 123
array3 needs to produce cba321

PS when I "cout<<array1;" right after the "cin>>array1;" it works but when done after prompting for second string it doesn't
and when I "cout<<array1[0],array1[1],array1[2];" the first element doesn't appear.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 int main() {
    const int arraySize = 3;
    const int finalArray = 6;
    char array1 [arraySize];
    char array2 [arraySize];
    char array3 [finalArray];
    
    greeting();
    
    cout<<"\nPlease enter the first 3 character string:";
    for (int k=0; k<arraySize; k++)
        cin>>array1[k];
    swap(array1[0], array1[2]);
    
    cout<<"\nPlease enter the second 3 character string: ";
    for (int j=0; j<arraySize; j++)
           cin>>array1[j];
    swap(array2[0], array2[2]);
    
    cout<<array1;
    cout<<array2;
    
    goodbye();

    return 0;
}
Last edited on
Use std::string rather than a character array. Always prefer a string over a char array unless there's some specific reason to use the array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Requires:
#include <string>

int main() 
{
    const int arraySize = 3;
    const int finalArray = 6;
    std::string one;
    std::string two;
    std::string three;
    greeting();
    
    cout<<"\nPlease enter the first 3 character string:";
    std::cin >> one;
    if(one.size() > arraySize)
         std::cout << "You inputted too much!"
    swap(one[0], one[2]);
    
    std::cout << one;
    
    goodbye();

    return 0;
}
Last edited on
ya the assignment is based on array manipulation.. thanks though
Use std::cin::get and <cstring> when manipulating C strings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstring>

int main()
{
   // need to account for the C strings' null terminator ('\0')
   const int arraySize { 4 };
   const int finalSize { 7 };

   std::cout << "Please enter the first 3 character string: ";
   char cstr1[arraySize];
   std::cin.getline(cstr1, arraySize);

   std::cout << "Entered: " << cstr1 << '\n';

   std::cout << "\nEnter the second 3 character string: ";
   char cstr2[arraySize];
   std::cin.getline(cstr2, arraySize);

   std::cout << "Entered: " << cstr2 << '\n';

   char cstr3[finalSize];
   
   // copy the first string to the final string
   // C11 added strcpy_s to prevent buffer overflows
   strcpy_s(cstr3, 7, cstr1);
   
   // concatenate the second string to the final string
   // C11 added strcat_s to prevent buffer overflows
   strcat_s(cstr3, 7, cstr2);

   std::cout << "\nThe final string is: " << cstr3 << '\n';
}

Please enter the first 3 character string: abc
Entered: abc

Enter the second 3 character string: def
Entered: def

The final string is: abcdef

Manually mucking around with C strings is not recommended, it is too easy to forget about the null terminator. Use the C library.

If you have to manually concatenate the two C strings substitute lines 24-30 with:
24
25
26
27
28
29
30
31
32
33
34
   for (int index { }; index < arraySize - 1; index++)
   {
      cstr3[index] = cstr1[index];
   }

   for (int index { }; index < arraySize - 1; index++)
   {
      cstr3[index + arraySize - 1] = cstr2[index];
   }

   cstr3[finalSize - 1] = '\0';
This is what is asked so I do not think i can use cstring

Write a program that will prompt the user for two strings. Your program should save these two strings in two different character arrays respectively. Create a third array which is used for merging these two arrays into one character array. The third array should contain all the characters from the first string + second string in a reverse order.
Create a third array which is used for merging these two arrays into one character array.

My second code snippet shows how to do that without the need for <cstring>. In forward ordering.

Rewriting the code for doing reverse ordering shouldn't be difficult.

The only question is what is the criteria for reverse ordering.

C string one is "abc", C string two is "def"

Should C string 3 be "cbafed" or "fedcba"?
"cbafed" ie string1(reverse)string2(reverse). I used what you suggested and it seems to work for the first 2 arrays but im having trouble making array3=array1+array2 (I know you cannot just add them).
having trouble making array3=array1+array2

My second code snippet does that. It replaces the elements of the 3rd C string with the elements from the other 2 C strings.

It concatenates 2 C strings into one larger C string.

Reverse order inserting elements from the 1st C string:

24
25
26
27
28
29
30
31
32
   for (int index { }; index < arraySize - 1; index++)
   {
      cstr3[index] = cstr1[ arraySize - 2 - index];
   }

   for (int index { }; index < arraySize - 1; index++)
   {
      cstr3[index + arraySize - 1] = cstr2[index];
   }

Please enter the first 3 character string: abc
Entered: abc

Enter the second 3 character string: 123
Entered: 123

The final string is: cba123

How would the 2nd for loop need to be modified to make the 2nd C string be inserted into the 3rd C string also in reverse order?

How would the 2 for loops need to be modified to make the 3rd C string contain a total reversal of the 2 other C strings?

You should have enough information to try rewriting the code yourself.
Topic archived. No new replies allowed.