Nov 25, 2020 at 3:55pm Nov 25, 2020 at 3:55pm UTC
So why aren't you using std::string for all this?
> if (S1[i] == c)
You need to record the value of i, so you can do the right strncpy() calls to extract bits of S1 to copy to S2.
Nov 25, 2020 at 5:12pm Nov 25, 2020 at 5:12pm UTC
If you really need to use char array, then to insert a , before a specified char consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
int main()
{
const size_t arrsize {50};
const char inschar {',' };
char S1[arrsize] {};
char S2[arrsize] {};
char c {};
std::cout << "Enter words (max 50 chars): " ;
std::cin.getline(S1, arrsize);
std::cout << "Enter specified element: " ;
std::cin >> c;
for (char *p1 = S1, *p2 = S2, *p3 = p2 + arrsize - 2; p2 < p3 && *p1; *p2++ = *p1++)
if (*p1 == c)
*p2++ = inschar;
std::cout << S1 << '\n' ;
std::cout << S2 << '\n' ;
}
Enter words (max 50 chars): qweryasdeedfez
Enter specified element: e
qweryasdeedfez
qw,eryasd,e,edf,ez
Last edited on Nov 25, 2020 at 5:12pm Nov 25, 2020 at 5:12pm UTC