benhpark
benhpark

Reputation: 37

How can I delete first and last characters of strings using member functions of std::string, without using remove_if, erase, etc?

EDIT: This question had been solved as of 1 PM on October 10, 2020. Thank you!

I was wondering how to remove the first and last special characters of a string variable "words", without touching the special characters in the middle and using remove, and erase-remove_if idioms. For example, the string @*Case-closed!! should return Case-closed, but the internal special characters must not be touched.

Specifically, I want to make use of find_first_of, find_last_of, length() [equivalent to size()]. Here is my attempted code but I am unsure how to go about setting the boolean condition for the for loop (or while loop). The code below is my attempt:

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main(){
    const string charsToSearchFor = "!@#$%^&*()_-+={}[]:;\"\'`<>,.\?/|\\";
    string words = "@**Man_Of_Steel...!";
    string forDisplay;
    
    int length = static_cast<int>(words.length()) - 1;
    cout<<"The string is "<<length<<" characters long."<<endl;
    
    int count1 = 0;
    int count2 = 0;
    bool foundOnFirst((int)words.find_first_of(charsToSearchFor) == 0);
    bool foundOnLast((int)words.find_last_of(charsToSearchFor) == length);
    bool isAlphabet((words[length] >= 'A' && words[length] <= 'Z') || (words[length] >= 'a' && words[length] <= 'z' ));
    
    cout<<"The first instance was found on "
        <<(int)words.find_first_of(charsToSearchFor)
        <<endl;
    cout<<"The last instance was found on "
        <<(int)words.find_last_of(charsToSearchFor)
        <<endl;
        
    cout<<"The boolean variable foundOnFirst should be true; actually it is: "
        <<foundOnFirst
        <<endl;
    cout<<"The boolean variable foundOnLast should be true; actually it is: "
        <<foundOnLast
        <<endl;    
    
    cout<<"This should be one; returns: "<<foundOnFirst<<std::endl;
    cout<<"This should be one; returns: "<<foundOnLast<<std::endl<<std::endl;

    //for deleting the first few special characters of a string; I have no clue
    //what Boolean condition would go into the second part of the for loop
    if (!isAlphabet || foundOnLast) {
        for (length = words.length(); !isAlphabet; length--) {
            //forDisplay = words.substr(1, length + 1);
            cout<<"The word in the loop process is: "<<forDisplay<<endl;
            cout<<"The length currently is: "<<length<<endl;                
            forDisplay = words.substr(1, length + 1);
            count2++;
        }//end if
    }
    
    //for deleting the last few special characters of a string; again, I have no
    //idea what Boolean condition would go into the second part of the for loop
    if (!isAlphabet || foundOnLast) {
        for (length = words.length(); !isAlphabet; length--) {
            //forDisplay = words.substr(1, length + 1);
            cout<<"The word in the loop process is: "<<forDisplay<<endl;
            cout<<"The length currently is: "<<length<<endl;                
            forDisplay = words.substr(1, length + 1);
            count2++;
        }//end if
    }
    
    cout<<"The word without the special characters is "<<forDisplay<<endl;
    return 0;
}

What would be your advice? Many thanks.

Upvotes: 1

Views: 494

Answers (2)

Shadi Alsalim
Shadi Alsalim

Reputation: 9

you can solve your problem with substr function just like this:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s = "substring";
    string d = s.substr(1,s.size()-2);
    cout<<d;
    return 0;
}

the first parameter is the index that you want to start with and the second one is the lenght of the new string so here the new string begins from the second letter in s and it takes (size of s)-2 so the output is "ubstrin" I hope that was useful

good luck!

Upvotes: 1

Minh
Minh

Reputation: 1725

You can combine find_first_of with find_first_not_of to delete the first special characters, and combines find_last_of with find_last_not_of to delete the last special characters.

Or if you want to delete until alphabet characters you can do one loop to search for the first and last alphabet characters, and substr that range.

Either way, maybe consider using 1 criteria only, alphabet, or blacklist

Upvotes: 0

Related Questions