Нow can i insert the desired character

I have such code of definition of the set character but I do not understand how to insert a comma over a certain character and to deduce it. You may want to use the function to copy the original line but with a comma before the specified element.

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
34
 #include <iostream>
#include <string.h>
using namespace std;
int main()
{
    setlocale(LC_ALL, "ru");
    char S1[50];
    char S2[50];
    char c;
    int i;
    bool f_is;
    cin.getline(S1, '\n');

    cin >> c;


    for (i = 0; i < strlen(S1); i++)
        if (S1[i] == c)
        {
            f_is = true;
            break;
        }

    if (f_is)
    {
        strcat(S1, S2);
        cout << "Символ " << c << " є в рядку" << endl;
    }
    else
    {
        cout << "Символу " << c << "немає в рядку" << endl;
}
}
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.
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
Thank you for your help.
Topic archived. No new replies allowed.