4 Replies - 2641 Views - Last Post: 26 April 2012 - 09:17 AM Rate Topic: -----

#1 DevNWF08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-April 12

consecutive characters in a string c++

Posted 25 April 2012 - 04:52 PM

I have an array of strings like
liiiike
nooot
mooveee

i need it to look like:
like
not
move

so far im able to compare one possition against another but there is something wrong with my code and i cant figure out what it is.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
const int MAX = 3;
int i,x;
string arr[MAX] = {"noww", "toooo", "nimmais"};


for(i = 0; i < MAX ; i++){ // i = number of strings in the array

int length = arr[i].length(); // length = number of character in each string, fixed number
 
	if(length > 0){
		for(x = 0; x < length; x++)//for loop NOT WORKING
		if(arr[i].at(x) == arr[i].at(2))
				{
				int remove = 1;
				
				arr[i].erase(2,remove);
				}
	}	
cout << arr[i]<< endl;
}


return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: consecutive characters in a string c++

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: consecutive characters in a string c++

Posted 25 April 2012 - 05:00 PM

First you should not use remove as a variable, because there is a function with that name already defined. Next ask specific questions, it doesn't work doesn't tell me anything. Why doesn't it work. Explain exactly what you are trying to do and tell use exactly what the program is doing.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 turboscrew  Icon User is online

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 628
  • Joined: 03-April 12

Re: consecutive characters in a string c++

Posted 25 April 2012 - 10:46 PM

So it's always the 2nd letter that is duplicated?

Or are you looking for something like:
        int y = 0;
        for(x = 0; x < length; x++)//for loop 
            if(arr[i].at(y) == arr[i].at(y+1))
                {
                int remove = 1;
                 
                arr[i].erase(y+1,remove);
                }
                else
                {
                   y++;
                }


Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: consecutive characters in a string c++

Posted 25 April 2012 - 10:52 PM

You are erasing characters within your loop, but the loop is dependent on the string's original length, can you see how this can be a problem? Try stepping through your code using a debugger and look at the variable "length" and then the actual length of your string for every iteration.

This post has been edited by jjl: 25 April 2012 - 10:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 DevNWF08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-April 12

Re: consecutive characters in a string c++

Posted 26 April 2012 - 09:17 AM

well I should be able to compare every letter in the word, if there are two same consecutive characters i should be able to erase one of them...I think if i am able to make an array out of the variable length i could just compare them
if (length[i] == length[i+1]), the problem is i dont know how :)

View Postturboscrew, on 25 April 2012 - 10:46 PM, said:

So it's always the 2nd letter that is duplicated?

Or are you looking for something like:
        int y = 0;
        for(x = 0; x < length; x++)//for loop 
            if(arr[i].at(y) == arr[i].at(y+1))
                {
                int remove = 1;
                 
                arr[i].erase(y+1,remove);
                }
                else
                {
                   y++;
                }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1