Well I ran it, but what is it your trying to achieve? What is the user supposed to enter when asked:
Char array: ?
Im also not very familiar with the gets() function, but I did do some quick reading and apparently its not a very good function to use.
A better alternative to gets(), is fgets().
Line 21. You are decrementing j but loop ends with j is equal to n. As a result, j goes from n-1 to negative-a-whole-lot until the program crashes.
The first time through the loop the code overwrites the null terminator.
At line 30 you output a single character - one past the last character in s. This is because in C++, defining char s[50] creates an array whose elements are s[0] through s[49].
Can you give an example of the expected output? For example, if the input is "this is a test" and the chacter to triple is 't' then what should the output be?
Keskiverto is right. The best way to do this is to use std::string, but if you must use a C-string then do it like this:
- go through the input string and compute the size of the output string
allocate the output string
-go back through the input string, copying to the output string. Use two pointers here, one for the source and one for the destination.
- Don't forget to include the null-terminator when you're done.