In this question you have to write a program consisting of two functions. A string has to be input in the main
function and then a string function, namely shorterString, has to be called to shorten the string by deleting
(“erasing”) every third character of the string. This has to be done repeatedly until the length of the string becomes
less than 6 and then the final string should be displayed by the main function. No global variables may be used.
Suppose, for example, the string
Hello world!
is input. Then
Helowold
should be returned to the main function by shorterString. Then, when the function is called again, the string
Heowld
should be returned. Then, when the function is called again, the string
Hewl
should be returned. This will be the final result (because its length is less than 6) and should be displayed.
Repeat the whole process described above for a number of strings until the string END is input. Use loops as
required. Run your program on the strings below and submit printouts of the program and output.
Cheers!
String manipulation is really not difficult.
Only two questions remain to be done.

Below is my code - but cannot get it to remove every 3rd character in a string - Help Please

using namespace std;
void shorterstring(string text)
{   
  for(int i =0; i <=0; i++)
  {text.erase(3,1);
  }
  cout<<text<<endl;
}

int main()                              
{    
        string sentence;
        
        cout << " Enter sentence: ";
        getline(cin,sentence,'\n');                      
       
        shorterstring(sentence);         
              
           
       return 0; 
}

also the logic is wrong in your code, with your current example only removes the character in the third position it does not remove every third character.

something like this creates close to what you are asking

// string_manip.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;
void removethird(string text) {
    cout << "Removing every third character until less than or equal to six characters remain" << endl;
    while (text.length() > 6)
    {
        for(int i = 1; i < int (text.length()); i++)
        {
            if (i % 3 == 0) // divisible by three 3,6,9,12,15,18...etc
            {
                cout << text.c_str() << endl;   //show me the string
                text.replace(i, 1, "\x0");      //replace the character with a null character
            }
        }
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    string foo = "HELLO WORLD";
    removethird(foo);
 return 0;
}

Hey!!!
The response from Killer_Typo was a good one - but what troubles me is the the for loop within the while
- should the i value start from 0 or from 1.
i.e the string array indexing would begin from 0 and not 1.
if it zero then the output from this program would be different from the actual output ( as reqd )

Pl. correct me if I am wrong in my understanding.

Ram Sharma

i started at 1 because the modulus of 0 and 3 will return 0 because zero divided by anything is zero and i do not want to remove the first character.

You end up removing the fourth character instead of the third one. Doing if(i % 3 == 2) would solve the problem.

But I still think that removing third character from each _word_ is different from removing third character from the string as a whole.

Hello World
Helo Wold
Heo Wod
He Wo

commented: oops thanks for fixing my mistake! :o) +5

You end up removing the fourth character instead of the third one. Doing if(i % 3 == 2) would solve the problem.

But I still think that removing third character from each _word_ is different from removing third character from the string as a whole.

Hello World
Helo Wold
Heo Wod
He Wo

awesome, i hadnt had the time to really check to see how accurate the work was i just understood the general concept.

i believe he wants to remove the third character from the string a whole not the words idividual

re-read his thread he does state

every third character of the string

so i went with that idea.

Removing third character from the string is pretty simple. I don't think an university assignment would be that easy. But I guess we would just have to wait for the OP to clear our doubts.

Just to clear up the air - When a string is inputed like below

Hello world! // The 3rd l, the space, the char r and the exclamation mark - must all be deleted and the string should the be like:

Helowold //then
Heowld //then
Hewl

Just to clear up the air - When a string is inputed like below

Hello world! // The 3rd l, the space, the char r and the exclamation mark - must all be deleted and the string should the be like:

Helowold //then
Heowld //then
Hewl

cool well now you have more than enough to begin work on your version, post your snippits along with questions and others along with myself will try to answer any questions :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.