remove a ponctuation from a string

hi guru;
your humble newbie is stuck somewhere with his own codes and he need some help form you.
i have to write a program that take string input and check if is there any ponctuation and remove them.

here my bit
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
#include <iostream>
#include <string>

using namespace std;

string upperToLower(string& my_input);

int main()
{
   string my_input;
   cout << "put a word" << endl;
   cin >> my_input;

   cout << upperToLower( my_input) << endl;
}             

string upperToLower(string& my_input)
{
    unsigned i = 0;
    string new_word;
    while( i < my_input.length())
    {
         my_input[i] = tolower( my_input[i]);
         if (ispunct(my_input[i]))
         {
             my_input[i] = ' ';
             
         }
         ++i;
    }
    return my_input;
}


in the code below i m printing my new string with some space if the user insert any ponctuation without grammatical respect
how can print the whole string without space in between.

thank you for help dear guru
Not sure what your tolower() on line 23 does

And I am going to assume ispunct() on line 24 returns true if its punctuation

but if you want to remove all punctuation from a sentence:
1
2
3
4
5
6
7
8
9
10
string upperToLower(string& my_input)
{
     for(int i=0; i <my_input.length(); i++)
     {
             if(ispunct(my_input[i])
                  my_input[i]= ' ';
     }

      return my_input;
}


If this is not what you are looking for please post more information like the errors you get.

Last edited on
tolower() changes a char to a lowercase value (if it is A-Z or a-z).

Also, you do realize that since you are passing my_input by reference you are actually editing the string...
thank you for help guru but my point his if someone writes something like this :

punct.uation

with that dot in the middle what shall we do for leave the space in between

thanks for your help again
dear guru firedraco,

thank you for reply but tolower() is one of the requierement of the homework that is not the problem. the problem is this one :

in this case

punct.uation

with that dot in the middle what shall we do for leave the space in between

thanks for your help again


what do mean by editing my_input . this something that i don't understand to pass by reference.
When you pass by reference, all the modifications on the argument will affect the variable passed
http://www.cplusplus.com/doc/tutorial/functions2.html
what about the ponctuation cas please Bazzy?
gen1mx6's function is ok but it is missing a ) at line 5
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>
using namespace std;

string upperToLower(string& my_input)
{
     for(int i=0; i <my_input.length(); i++)
     {
             if(ispunct(my_input[i]))
                  my_input[i]= ' ';
     }

      return my_input;
}

int main()
{
      string my_input = "this.string,needs?to,be;changed...";
      upperToLower(my_input);
      cout << my_input;

      return 0;
}

You will see that the value of my_input will be changed by the function
Last edited on
Ok I understand what you want to do now.

check this link out:

http://www.bgsu.edu/departments/compsci/docs/string.html

There is a method called "Substring removal" in the String class-
1
2
3
4
5
string str = "punct.uation";
// first parameter is the position in the string to erase, second parameter is how many characters you want to erase
str.erase(4,1);

cout<<str<<endl;// will cout "punctuation" 


so your if statement will look like
1
2
if(ispunct(my_input[i]))
          my_input.erase(i,1);

Last edited on

i bau infront you guru gen1mx6
thank you guru gen1mx6 problem solve
Topic archived. No new replies allowed.