Strip Non-Alpha Characters from Both Ends of String, Leave Non-Alpha Characters Inside String Alone

I am trying to write a program that strips non-alpha characters from both sides of a string, but leave the non-alpha character(s) inside the string alone.

Here is the code I have written

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
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>
using namespace std;

string nonAlpha(string input)
{
    stringstream ss;
   for (int x = 0; x < input.size(); x++)
   {
       if(isalpha(input[x]))
       {
           ss << input[x];
       }
   }

    if (ss.str().length() > 0)
        return ss.str();
    else
        return "Nothing left";

}

int main()
{
    string userInput;
    int counter = 0;

    cout <<"Enter a word" <<endl;
    cin >> userInput;

    for(int i = 0; userInput[i] != '\0'; i++)
    {
		userInput[i] = tolower(userInput[i]);
    }

    for(int i = 0; i < userInput.size(); i++)
    {
        while(!isalpha(userInput.at(i)))
        {
            counter--;

        }
        cout<<userInput.substr(i)<<endl;
        return 0;
    }
    //cout << userInput <<endl;

}


Whenever I type in a word such as 'the,' the string prints out fine. Whenever I enter in a string with a non-alpha character the runtime is really slow, and nothing prints out. My question is, what can I do to help fix my problem? Thanks.
Maybe you should actually try calling the function?

1
2
3
4
5
6
7
8
9
10
int main()
{
    string userInput;
    int counter = 0;

    cout <<"Enter a word" <<endl;
    cin >> userInput;

    cout << nonAlpha(userInput) ;
}
Use a container like the assignment says, pop_front() and pop_back() if the chars aren't alpha chars.

See you at Hancock tomorrow...
There is a function for that in boost:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/classification.hpp>
int main()
{
    std::string str = "111hi___";
    trim_if(str, ! boost::algorithm::is_alpha());
    std::cout << str << '\n';
}

online demo: http://liveworkspace.org/code/5484520f350d2721ef477486f3677763
@someone2


The name of your function confuses users. Instead of nonAlpha it would be better to use something as trimNonAlpha.
Inside the function body you remove all non-alpha cheracters from the original string. That is not you wanted to do.
Also there is no a great sense to use std::stringstream.
All you need is to find the first alpha character and the last alpha character in the string.

1
2
3
4
5
6
7
8
9
10
11
std::string trimNonAlpha( const std::string &s )
{
	std::string::size_type pos = 0;
	std::string::size_type n   = s.size();

	while ( pos < n && !std::isalpha( s[pos] ) ) ++pos;

	while ( n != pos && !std::isalpha( s[n - 1] ) ) --n;

	return ( s.substr( pos, n - pos ) );
}
Last edited on
Topic archived. No new replies allowed.