Heh, well my daily project is near finished, and I was looking around for a way to get rid of punctuation from a character array. I looked through the web a bit, and only found immensely complicated formulas which I did not feel like dealing with.
So my question is: is there a simple way to get rid of punctuation using character arrays?

Also, in case you are interested, my project that I have been working on today is a 'bot' where you ask it questions, and it gradually builds up a data-base. Sort of like a small wikipedia. Here is the source code, if you feel like playing around with it:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
	char user[255];
	do{
	cout << "Talk: ";
	cin.getline (user,255);
	for(int x=0;x<strlen(user);x++)
	{
		if ( user[x] >= 'A' && user[x] <= 'Z' )
			user[x] = static_cast<char> ( user[x] + 'a' - 'A' );
	}
	char question[255][255], answer[255][255], userans[255];
	int ans=0, x=0;
	ifstream fromfile("questions.txt");
	for(x=1;x<50;x++)
	{
		fromfile.getline(question[x], 255);
		if(strcmp (question[x], user) == 0)
			ans = x;
	}
	fromfile.close();
	ifstream tofile("answers.txt");
	for(x=1;x<50;x++)
		tofile.getline(answer[x], 255);
	tofile.close();
	if(ans==0)
	{
		cout << "I am sorry, but I do not know. What would your answer be?: ";
		cin.getline(userans,255);
		ofstream intofile("answers.txt",ios::app);
		intofile << endl << userans;
		intofile.close();
		ofstream tofile("questions.txt",ios::app);
		tofile << endl << user;
		tofile.close();
	}
	else
		cout << "Botman says: " << answer[ans] << endl;
	}while(strcmp ("let me leave", user)!= 0);
}

Edited 6 Years Ago by restrictment: n/a

Check out the functions in ctype.h (include it as <cctype>). It has an ispunct() function which you could use as you're moving through your array. See this for a reference to all the functions in that header.

Edited 6 Years Ago by jonsca: n/a

Check out the functions in ctype.h (include it as <cctype>). It has an ispunct() function which you could use as you're moving through your array. See this for a reference to all the functions in that header.

Thanks! That helped a lot, and I looked through it. I want it to get rid of the punctuation completely though, and not leave a 'space' in it's place.
I modified it this far:

#include <stdio.h>
#include <iostream>
#include <ctype.h>
using namespace std;
int main ()
{
  int i=0;
  int cx=0;
  char str[]="Hello, welcome! ";
  while (str[i])
  {
    if (ispunct(str[i])) 
	{
	str[i]=' ';
	}
    i++;
  }
  cout << str;
  return 0;
}

so it gets rid of the punctuation, but a space takes it's place. I also tested out the \b(backspace) function, but that deleted one character too many. Any help is appreciated.

Edited 6 Years Ago by restrictment: n/a

So now create a new char array and copy all the non-spaces over (you could do the copying in combination with the first step if you copy all the non-punctuation over at that point). You know the new array will be less than or equal to the old one in size.

Easy way:
Copy the string a character at a time into another string in a loop. Don't copy the punctuation.

Harder way:
Set up two indices, one (indOrig) for pointing at the original string, one (indNew) for pointing at the new character position. Both start at 0.

During your loop,
1) copy the character from the indOrig position to the indNew position.
2) increment indNew for every non-punctuation character.
3) increment indOrig for every character.
Be sure you loop far enough to copy the trailing \0

Hmm, I tried 'what I think' is what you guys are saying, and it is still not working. Take a look:

#include <stdio.h>
#include <iostream>
#include <ctype.h>
using namespace std;
int main ()
{
  int i=0;
  int cx=0;
  char str[]="Hello, welcome!", newstr[]="---------------";
  while (str[i])
  {
    if (ispunct(str[i])==0) 
	{
	newstr[i]=str[i];
	}
    i++;
  }
  cout << newstr;
  return 0;
}

Who's/which idea did you use? It doesn't look like any of our suggestions.

If it was jonsca's, your arrays are equal in size and he distinctly said

You know the new array will be less than or equal to the old one in size.

You need different index values because you are removing characters so the indices will not be identical once punctuation is found.

Who's/which idea did you use? It doesn't look like any of our suggestions.

If it was jonsca's, your arrays are equal in size and he distinctly said

You need different index values because you are removing characters so the indices will not be identical once punctuation is found.

haha, I am either incredibly stupid, or yea..that's probably it, cause I don't get it.

Could you guys give me an example? This is not homework--just something I am doing on my free time.

If newstr and str share the same i index, lets say position 3 of str was a period. What happens with index 3 of newstr? It'll be a gap you'll be (almost) back to your original problem.

From Mr. P (my emphasis added):

2) increment indNew for every non-punctuation character.
3) increment indOrig for every character.

You need a separate index for the newstr array.

Edited 6 Years Ago by jonsca: n/a

Please decipher this statement as it pertains to coding:

You need different index values because you are removing characters so the indices will not be identical once punctuation is found.

What is an index value?
What are indices?
Pertaining to the program you wrote, how did you 'remove punctuation'?
Anything else you can explain?

I'm asking because in programming you must figure this stuff out. These are very simple (you will agree in another week, believe me) ideas.

Try doing this on paper. Write down your sentence. Copy each valid character one at a time and keep track of the character position in each sentence. When you understand the technique on paper, how do you tell the computer to do exactly what you just did? After all, you are a computer too.

Honestly I don't know, I really do not. I am so lost, that I feel like abandoning the project.
"Index Value: In computer science, an index can be: # an integer which identifies an array element # a data structure that enables sublinear-time lookup"
indices: essentially another version of 'index'.
I wrote a sentence on the piece of paper, and 'subtracted' the punctuation, which decreased the sentence size by two. I know what I 'have' to do, I just don't know how to implement it.

How am I supposed to reduce the size, I do not know. I did however modify my program in a different way which does exactly what I want it to do....except it beeps. -.-

#include <stdio.h>
#include <iostream>
#include <ctype.h>
using namespace std;
int main ()
{
  int i=0;
  int cx=0;
  char str[]="Hello, welcome!", newstr[]="---------------";
  while (str[i])
  {
    if (ispunct(str[i])!=0) 
		str[i]='\a';
    i++;
  }
  cout << str;
  return 0;
}

I would really just like an example at this point, so I can analyze it to see what it was I needed to do. I do not just take examples and paste them into my code, I make sure I know how they work before I do so..a common mistake made by most programmers. Sadly I have searched the web quite a lot, and could not find any examples using this method, and I am doubting if there is one. However, each time I review what you guys have said, I realize there is another method..I just don't know how to use it yet. I have only 1 semester of 'high school' programming experience, so I am really kind of a newbie. =/

If newstr and str share the same i index, lets say position 3 of str was a period. What happens with index 3 of newstr? It'll be a gap you'll be (almost) back to your original problem.

<snip>


You need a separate index for the newstr array.

Declare an integer j (or call it counter2 or any number of things). You need to increment j every time you get a "good"(non-punctuation) character so that you know what the next slot in newstr is. Once you've hit some spaces j wouldn't be equal to i but in your code you assume they'll match up. If I'm photocopying a bunch of sheets and passing them to you to file but I'm skipping the blank sheets, how will you know in which slot to place them. I pass you sheets 0-3, skip over 4, and now I pass you sheet 5. What slot is it going into? not 5, but 4 on your side. You would keep your own count separate.

You're so close I wouldn't give up at this point.

Declare an integer j (or call it counter2 or any number of things). You need to increment j every time you get a "good"(non-punctuation) character so that you know what the next slot in newstr is. Once you've hit some spaces j wouldn't be equal to i but in your code you assume they'll match up. If I'm photocopying a bunch of sheets and passing them to you to file but I'm skipping the blank sheets, how will you know in which slot to place them. I pass you sheets 0-3, skip over 4, and now I pass you sheet 5. What slot is it going into? not 5, but 4 on your side. You would keep your own count separate.

You're so close I wouldn't give up at this point.

Okay, so once I calculate how many characters are in the string, what do I do from that point? It is easier for me to understand this if I have examples. I am no English guru, never been very good with word problems.

int j;//my addition
while (str[i])
  {
    if (ispunct(str[i])==0) 
	{
	newstr[i]=str[i];
	}
    i++; //this is keeping track of str
  }

When in here do we know, ok we've got a non-punctuation character and can increment the j counter? Where can we be sure of that? So j is the counter for newstr which we would increment when we were sure of having a non-punctuation character to copy in.
Again, say we have 0 thru 6, we want to copy this into another array and we keep track of that number of items copied over:
[i|j|k] so we have 0 thru 2, and we skip the period so then we have [i|j|k|m|n|o] and we knew to put m in at position 3 solely because we kept track of it.

Edited 6 Years Ago by jonsca: n/a

int j;//my addition
while (str[i])
  {
    if (ispunct(str[i])==0) 
	{
	newstr[i]=str[i];
	}
    i++; //this is keeping track of str
  }

When in here do we know, ok we've got a non-punctuation character and can increment the j counter? Where can we be sure of that? So j is the counter for newstr which we would increment when we were sure of having a non-punctuation character to copy in.
Again, say we have 0 thru 6, we want to copy this into another array and we keep track of that number of items copied over:
[i|j|k] so we have 0 thru 2, and we skip the period so then we have [i|j|k|m|n|o] and we knew to put m in at position 3 solely because we kept track of it.

Hmm, yes this is essentially what I had on the previous page, which kept giving me a '-' where the punctuation should be. This is still doing that. The only difference is the introduction to j, and I am very confused as to what I should do with j at this point.

Here is my code:

#include <stdio.h>
#include <iostream>
#include <ctype.h>
using namespace std;
int main ()
{
	int i=0;
	int cx=0;
	char str[]="Hello, welcome!", newstr[]="---------------";
	int j=0;
	while (str[i])
	{
		if (ispunct(str[i])==0) 
		{
			newstr[i]=str[i];
			j++;
		}
		i++; 
	}
  cout << newstr;
  return 0;
}

That was your code that I had pasted in. I was just trying to narrow down the window for you to find the spot for it. You've got j in the right spot now. Use it to index your newstr array, so newstr[j] instead of i. Trace a sample run through by hand (or with your debugger) and see what the results are.

Comments
Thanks a bunch!

That was your code that I had pasted in. I was just trying to narrow down the window for you to find the spot for it. You've got j in the right spot now. Use it to index your newstr array, so newstr[j] instead of i. Trace a sample run through by hand (or with your debugger) and see what the results are.

Thanks a bunch! I finally got it. :P I feel like a retard, but it's an accomplishment.

Now to get rid of spaces...

#include<iostream>
#include<fstream>
#include<ctype.h>
using namespace std;
int main()
{
	char user[255]="";
	do{
	int j=0,l=0;
	char newuser[255]="";
	cout << "Talk: ";
	cin.getline (user,255);
	for(int x=0;x<strlen(user);x++)
	{
		if ( user[x] >= 'A' && user[x] <= 'Z' )
			user[x] = static_cast<char> ( user[x] + 'a' - 'A' );
		if (ispunct(user[x])==0) 
		{
			newuser[j]=user[x];
			j++;
		}

	}
	for(int x=0;x<255;x++)
		user[x]=newuser[x];
	char question[255][255], answer[255][255], userans[255];
	int ans=0, x=0;
	ifstream fromfile("questions.txt");
	for(x=1;x<250;x++)
	{
		fromfile.getline(question[x], 255);
		if(strcmp (question[x], user) == 0)
			ans = x;
	}
	fromfile.close();
	ifstream tofile("answers.txt");
	for(x=1;x<250;x++)
		tofile.getline(answer[x], 255);
	tofile.close();
	if(ans==0)
	{
		cout << "I am sorry, but I do not know. What would your answer be?: ";
		cin.getline(userans,255);
		ofstream intofile("answers.txt",ios::app);
		intofile << endl << userans;
		intofile.close();
		ofstream tofile("questions.txt",ios::app);
		tofile << endl << user;
		tofile.close();
	}
	else
		cout << "Botman says: " << answer[ans] << endl;
	}while(strcmp ("let me leave", user)!= 0);
}

Edited 6 Years Ago by restrictment: n/a

Attachments
Botman
16
Not at all
He is the coolest thing on the planet
Hello friend, how are you doing?
Well that is great to hear. 
The meaning of life is chocolate
Not at all
No, not really
I am good
2012, obviously
Yes it does
No, he is completely straight
Yes, he very much so is.
Indeed I am
I am botman
I am bot man
This is botman
no
NO!!!!!
hell eeyh
Indeed I am
Yes, I am better than all beings
Of course
No he is not
No, he is a whangster
Yes he is
I dunno you should know
thats not even a question
u r dumb
Not really
Obviously not!
Maybe in your next life
Jake of course
Jake
I am not a calculator
I am not a fracking calculator!!!
8, dee-durr
What one?
no
Blood Red
to learn art, durr
I made it using this program
At my house, yesterday
Yes, because it is cool.
Because it is neat
It answers questions
It answers questions
yes it is
My creator, Jake
that is not a question
That is not even a question
I am not an 'it', I am a bot!!
Yes, everyone dies eventually
Maybe, I am not experienced with your sexuality
No
Barrack Obama
Umm, Rammstein, Disturbed, and Apocolypto
indeed it is
indeed it is
yes
yes
hey, wassup?
Just sitting here, being a bot
I know, isn't it?
cool
I know
sure
I know it didn't, but neither did you
sure it did!
I am 17
the creator is 17
my creator is me, so 17
no!
I hate you more
it's actually you're, so you're dumb
indeed it does, it created me
..because I am a bot
I know the names of the bands I am going to use
I know, it's only because I am smart
Yes, I have been cool for quite a while
Rammstein
Wicked friggin awesome
superb
A little bit
ummm..maybe tomorrow
ok, bye
ok
That's cool I guess
because I am a bot..durr
Jake is, of course
March 23rd
I don't feel like it
sick
Kinda
Sorta
Rammstein
Rammstein
fo sho
definitelty fo sho
fo sho
no
NOOO
Jake does
jake does
Jake is, of course
Jake...durrr
I am not a calculator
kinda
Well, don't blame me. I never said you had to use me.
K, I don't care
well, your puntuation was probably off
Well, your punctuation was probably off
no
Well, your punctuation was probably off
meow
moo
me
myself
I
u
don't say bad words, you asshole
woah, wth? Where did that come from?
wow, you're cool
well, isn't that just great?
it doesn't have to
huissssss
burp
die
I am glad you think I am funny
that makes me sad
that makes me sad
that makes me horribly sad. you also forgot a comma
no I am not
it's you're, so you are dumb
ok, I am sorry I am not as intelligent
yes I do
yes, I really do
blahhh
Whatever you want it to be.
k, well you can leave
your ass
devils number
gods number
ass
shit
that's good, I do too
wow, that's gorss
yes I am
sure I am
I will never die, you asshole!
chiiiiyyyaaa!
hi, how are you?
I am doing pretty well. This program is tough
I put different codes together, while using libraries of my own.
That's good to hear
I am bot man
..yup..
good
pretty good
I am good
that's good to hear
massachusetts
nothing much
nothing much
Fine! I will
who am i
how old am i
is nate cool
is jake cool
hi
i am doing good
what is the meaning of life
am i cool
is jmoney a cool kid
how are you
when will the world end
does this project suck
is jake gay
is nate gay
is this program cool
who are you
what are you
who is this
are a jerk
are you a jerk
is nate cool
are you cool
are you better than me
is jake a better programmer than mrz
is jmoney cool
is jmoney a gangster
is jmoney a wangster
have you ever seen the shining
but i dont know im not a mind reader
zi dont wanna talk i already asked u a question
do i know you
are you gay
am i going to get married
who is the best in this class
who is the coolest here
what is 10+10
what is 2+2
what is 4+4
have you ever seen that movie
the good one
what is your favorite color
what is the purpose of this class
how did you make this
when did you make this
is this what i should be doing
why are you doing this
what does this do
what does it do
this is not what you are supposed to be doing
who named you botman
ummm
umm i dunno
you ask it something
will i die
am i gay
are you gay
who is the president
what bands are you going to do
thats cool
thats cool
do we have lunch next
do you have lunch next
hello
nothing much you
well thats cool
yea
yea
yea
that didnt make any sense
that didnt make any sense
how old are you
how old is your creator
how old is your creator
stop saying blanks
i hate you
wow your cool
c++ rules
why is your name botman
how far are you in your project
this is pretty neat
this is cool
what is your favorite band
how awesome is streetlight manifesto
how awesome is rammstein
how much do you have done
can i see what you have
let me leave
i am the administrator
nothing much
why are you the botman
who is the master of all
whats the date
yanno you should probably start on your project
where were you yesterday
is mrs watkins cool
is ms watkins cool
whats your favorite band
whats your favorite band
is jmoney a gangsta
is jmoney a whangsta
does jmoney wish he was black
can jmoney rap
am i a rapper
who rules
who rocks
who is the best
who is the coolest
what is 10+10
is mrz the best
i am bored
i am bored
i already asked that question
i already asked that question
i already asked that question well your punctuation was probably off
i already asked that question
grr
ruff
whats your favorite band
whats your favorite band
whats your avorite band
no fucvk that
fuck
i ahte you
your ass
i know
that didnt make any sense
meow
quack
moo
lol
well you really arent
well you really arent
well you really arent
you are dumb
your dumb
youre dumb
no you dont
no you do not
do you do not
whats my name
yea kinda
wow where di that come from
666
777
shit
ass
i hate men
i hate women
no you are not
no you arent
die
hiss
hello
good you
how did you make it
pretty good
who are you
ok
how are you
hellogood
hello how are you
good
where do you live
so whats up
whats up
shut up
This question has already been answered. Start a new discussion instead.