473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to remove periods , punctuation from cstring

77
len = strlen(in);
if (in[len-1] == '.')
in = in.TrimRight('.');


you have to declare len, and use cstring for in...

this took me forever lol and its so simple.. i need to set it up for ! points now...

sorry if this is not relevant it took me weeks to figure this out lol and im excited
Jul 31 '07 #1
9 5068
jerger
77
punk is int, len is int, in is cstring

//punctuation removal
//while loop checks several times to catch all punctuation

while (punk < 10){
len = strlen(in);
if (in.Left(1) == '"')
in = in.TrimLeft('"');

if (in[len-1] == '.')
in = in.TrimRight('.');

if (in[len-1] == '"')
in = in.TrimRight('"');

if (in[len-1] == '!')
in = in.TrimRight('!');

if (in[len-1] == ',')
in = in.TrimRight(',');

if (in[len-1] == '?')
in = in.TrimRight('?');

if (in[len-1] == ':')
in = in.TrimRight(':');

if (in[len-1] == ';')
in = in.TrimRight(';');

punk++;
}
punk = 0;
//end of punctuation check
Jul 31 '07 #2
jerger
77
errr only problem is if i enter like 30 periods it crashes the program 0.o
Jul 31 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Why not just copy the string to a temp and skip the punctuation characters. Then copy the temp back to the original.

??

I mean, you have already done a strlen() so that's the read part of the copy right there.
Jul 31 '07 #4
jerger
77
can you expand on this? i'm a little confused. what do you mean by "skip the punctuation"??? how can i remove it then? thanks!

how can i ignore the chars while making the temp? sounds like a better idea with less if statements
Jul 31 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. char str[] = "H%e*llo.(*&"
  2. char temp[100];
  3.  
  4. while (str[i] != '\0')
  5. {
  6.       switch (str[i])
  7.       {
  8.            case '.':
  9.                   break;
  10.            case '&':
  11.                   break;
  12.            case etc... the other puncuation marks
  13.                   break;
  14.            default:
  15.                   temp[i] = str[i];
  16.       }
  17.       ++i;
  18. }
  19.  
When you leave the loop add a \0 to temp and strcpy(temp, str).
Jul 31 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
There is also an ispunct() function in stdlib.h that you can use if you want to omit all punctuation instead of specific punctuation.
Jul 31 '07 #7
jerger
77
interesting!!! how could i incorporate that?

well i made a double case statement and it works awesome for now with no memory overflows or crashing:

//punctuation removal
//case statement loops until punctuation is moved
//this prevents error where only one punctuation is fixed and it moves on, this catches quotes and punc."
//default is when input is good/cleaned

len = strlen(in); //sets len as a position for the last letter in



switch (in[0]) //punc removal
{
//case '" (in.Left(1) == '"')
//in = in.TrimLeft('"');

case '!' : in = in.TrimLeft('!');
// break;
case '.' : in = in.TrimLeft('.');
// break;
case '?' : in = in.TrimLeft('?');
// break;
case '"' : in = in.TrimLeft('"');
// break;
case ';' : in = in.TrimLeft(';');
// break;
case ':' : in = in.TrimLeft(':');
// break;
case ',' : in = in.TrimLeft(',');
// break;

default : //break;
len = strlen(in);
switch (in[len -1]){ //punc removal

//case '"' : (in.Left(1) == '"');
//in = in.TrimLeft('"');

case '!' : in = in.TrimRight('!');
// break;
case '.' : in = in.TrimRight('.');
// break;
case '?' : in = in.TrimRight('?');
// break;
case '"' : in = in.TrimRight('"');
// break;
case ';' : in = in.TrimRight(';');
// break;
case ',' : in = in.TrimRight(',');
// break;
case ':' : in = in.TrimRight(':');
// break;
default : //this is like a end loop, once it removes punctuation it moves on

break;
}
}
Jul 31 '07 #8
JosAH
11,448 Expert 8TB
You really should have a look at the ispunct() function in ctype.h/cctype. It's as
easy as this:

Expand|Select|Wrap|Line Numbers
  1. char* remove_punct(char* dest, char* string) {
  2.  
  3.    char* t;
  4.    for (t= dest; *t= *string++; t+= !ispunct(*t));
  5.    return dest;
  6. }
kind regards,

Jos
Jul 31 '07 #9
jerger
77
how can i make that work with a cstring, and in c++? it currently works pretty well/fast but only for like 8 punctuations
Aug 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Joseph | last post by:
I have a textBox that people writes stories in it. They can use for format. I have Aspell installed on the server, so people can make correction to their text. Sometimes, they forget to add a...
7
by: Lachlan Hunt | last post by:
Hi, I have recently downloaded and experemented with IBM HPR 3.0, and Opera 8 with text-to-speech, and have come to realise some fairly annoying issues regarding punctuation marks. I've found,...
3
by: MLH | last post by:
I have a block of text with about 19,000 characters - alphanumeric, punctuation, hard returns, etc... I would like to count the number of periods ( Chr$(46) ) appearing in the document. Whats a...
4
by: ZhangZQ | last post by:
how to remove the apply and help button from propertysheet? I tried to this->m_psh.dwFlags &= ~PSH_HASHELP; this->m_psh.dwFlags |= PSH_NOAPPLYNOW; this->m_psh.dwFlags |= PSH_NOCONTEXTHELP; ...
4
by: Kun | last post by:
i have an html/cgi input that takes in values to a mysql database, however, if i stick in $20 instead of 20, it crashes the program because of the extra $ sign. I was wondering if anyone has a...
6
by: Tashfeen Bhimdi | last post by:
I'm trying to remove punctuation from a string with the following code: ---------------------------- #include <string> #include <algorithm> #include <cctype> .. using namespace std ..
5
by: joe | last post by:
hello i have a databse program that uses char arrays to output data to reports. I would like to remove all invalid characters from the array and replace them with a blank space. I have problems...
9
by: Donos | last post by:
I have a CString with 100 characters. Now i want to make that to 2 lines. For example, CString str = "This is just a test to find out how to break a cstring"; I want this CString in the...
10
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.