Hi,
I'm making a code that looks through an input string for double letters, and whether the number of characters is even or odd. If there are two identical characters next to each other, the program puts "XYXY" in between them, and if the number of characters is odd, it adds "WXW" at the end (three, to make it even).
Now, the code works, I would say, except in the output there are some strange box-like symbols at the very end. That is, when I tell it to cout the final string, it prints the correct one, but with some strange symbols at the end.
Here's my code:
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
char key[100];
cin.getline(key, 100, '\n');
const char ins1[]="X";
const char ins2[]="Y";
const char ins3[]="W";
int length = strlen(key);
cout << "Length: " << length << endl;
int i; int j=0; int count=0;
char prepared[1000];
for(i=0;i<(length);i++)
{
if(key[i]==key[i+1])
{
prepared[j]=key[i];
prepared[j+1]=ins1[0];
prepared[j+2]=ins2[0];
prepared[j+3]=ins1[0];
prepared[j+4]=ins2[0];
j=j+5;
count=count+1;
}
else
{
prepared[j]=key[i];
j=j+1;
count=count;
}
}
if(length % 2 == 1)
{
prepared[j]=ins3[0];
prepared[j+1]=ins1[0];
prepared[j+2]=ins3[0];
j=j+3;
}
cout << "Total number of doubles: " << count << endl;
cout << "Total number of characters in prepared string: " << j << endl;
cout << "Testing the string: " << prepared << endl;
return 0;
}
|
And the output:
1 2 3 4 5 6 7
|
peder@peder-ThinkPad-X220:~/C++/testing$ ./a.out
odd
Length: 3
Total number of doubles: 1
Total number of characters in prepared string: 10
Testing the string: odXYXYdWXW]�_
peder@peder-ThinkPad-X220:~/C++/testing$
|
I'm not sure if those symbols are even write-able here, but they are squares with four numbers in them. Like a tiny 2x2 matrix. Maybe it's super obvious what that means, but I'm still new to C++, so I don't know.
Anyway, what confuses me is that it counts the number of characters right (j here, which is printed at the end), but still these weird ones pop up.
Does anyone know what this is?
Thanks!