Hi,
I'm making a loop that runs through a string of characters and looks for double letters. The searching procedure works fine, e.g. for the input "pepper" it finds one double (a pair of adjacent, identical characters), and for the input "nooob", it finds two pairs. Now, the thing I actually want it to do is to break up these pairs by inserting a couple of characters in between them.
What I've done so far is that I create a new string that's empty, and that fills in the characters from the input string. Whenever (in the loop over the characters from the input) it detects a double (again, the detection works), it should, instead of just adding the two letters, say "pp" to the new resulting string, it should add "pDOUBLEp". My problem is that when I try to make it do that, I get an error about me trying to convert *char to char, which I didn't even know that I did.
Anyway, here's the 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
|
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
char key[100];
cin >> key;
char ins1[]="X";
char ins2[]="Y";
char ins3[]="W";
int length = strlen(key);
int i; int j=0; int count=0;
char prepared[1000];
for(i=0;i<(length-1);i++)
{
if(key[i]==key[i+1])
{
prepared[j]=ins1;
prepared[j+1]=ins2;
prepared[j+2]=ins3;
j=j+4;
count=count+1;
}
else
{
prepared[j]=key[i];
j=j+1;
count=count;
}
}
cout << "Total number of doubles: " << count << endl;
cout << "Testing the string: " << prepared << endl;
return 0;
}
|
And the error message when I try to compile:
1 2 3 4
|
peder@peder-ThinkPad-X220:~/C++/testing$ g++ prepareTest.cpp
prepareTest.cpp: In function ‘int main()’:
prepareTest.cpp:24:18: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
|
* Note that in the code I ran to get this I had switched two of the "inserts" for something else, hence only one error.
Does anyone see what's obviously wrong here?
Thanks!