469,352 Members | 1,661 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

remove punctuation in C

7
hey,
can anyone help me write up a function to remove space, semicolons, etc?
ty.
Nov 21 '08 #1
15 12825
donbock
2,422 Expert 2GB
Do you have any suggestions for how you might recognize punctuation or space characters in a string?
Nov 21 '08 #2
kpdp
7
so, i think when you are using the remove_punc function, you have to write the script so that it keeps in the characters, while removing the punctions.
Nov 21 '08 #3
oler1s
671 Expert 512MB
so, i think when you are using the remove_punc function, you have to write the script so that it keeps in the characters, while removing the punctions.
Yes, presumably itís why you called it a remove punctuation function, as opposed to retain punctuation function or something. We arenít sure what your question is though. You requested help, but thatís not necessary. We are all here to help.

But what is your question?
Nov 21 '08 #4
kpdp
7
I'm sorry. I have just started learning programming so I am trying to teach myself the basics with functions and strings.
My question is that how do i write something like "My name is Peter" and I get back "mynameispeter" ?
How do i use the function?
Nov 21 '08 #5
donbock
2,422 Expert 2GB
I'm sorry. I have just started learning programming so I am trying to teach myself the basics with functions and strings.
My question is that how do i write something like "My name is Peter" and I get back "mynameispeter" ?
How do i use the function?
First let's specify the interface to this function. It needs an input string and it needs to return a string. The input string is easy to handle: one of the function arguments is a pointer to the input string. I can think of three ways to handle the output string: (1) since the length of the output string can never be greater than the length of the input string, you can overwrite the input string; (2) you can malloc a buffer for the output string; and (3) the caller can pass you a buffer to use for the output string. Options (2) and (3) have failure modes (malloc can fail in #2; and the passed buffer can be too small in #3), so some method is needed to distinguish success from failure. Which of these sounds like what you want to do? I suggest you pick an interface and then create a corresponding function prototype.
Nov 21 '08 #6
oler1s
671 Expert 512MB
My question is that how do i write something like "My name is Peter" and I get back "mynameispeter" ?
Thereís two aspects to that question. One is the issue of code: what exact code do you need to write. The other issue is the algorithm, which is partly independent of the language and code. How do you approach something like this algorithmically? So you need to sit and think about algorithms. What data structure are you dealing with? How would you do the work on paper? What does pseudocode or plain English instructions for the algorithm look like? Thatís your primary question.
Nov 21 '08 #7
@kpdp
I haven't done this in C but have done this in C++, java and C#.

psuedo code:

string someFunction(string string){
string str;
for(int i = 0 ; i<string.size() ; i++) {//loop through the main string passed in character by character
char j = string[i];
if(j>=65 && j<=90 || j>=97 && j<=122 || j == 32 || j>=48 && j<=57){ //these numbers are ascii values... 65-90 lowercase letter, 97-122 upper case letter , 48-57 numbers and 32 is spaces
str += string[i];
}

return str;
}

this is a c++ code... it may even compile. if called correctly....

by for string manupilations ascii values are the way to go!

i'm not a pro but this algorythm should work in c as well!

drjay
Nov 23 '08 #8
JosAH
11,448 Expert 8TB
@drjay1627
No they are not.

kind regards,

Jos
Nov 23 '08 #9
@JosAH
why not??? what do you suggest?
Nov 23 '08 #10
boxfish
469 Expert 256MB
@drjay1627
Because the ASCII values of characters may be different on different platforms. It is safer to use the actual letters, like this:
Expand|Select|Wrap|Line Numbers
  1. char j = string[i];
  2. if((j >= 'a' && j <= 'z') || (j >= 'A' && j <= 'Z') || j == ' ' || (j >= '0' && j <= '9')) {
  3.     str += string[i];
  4. }
But the best thing to do is use isalpha() and friends. I think they're in stdlib.h, or cstdlib.
Expand|Select|Wrap|Line Numbers
  1. char j = string[i];
  2. if(isalpha(j) || isspace(j) || isdigit(j)) {
  3.     str += string[i];
  4. }
islower() and isupper() may be useful too.
Hope this helps.
Edit:
Also check out ispunct().
Nov 24 '08 #11
@boxfish
thanks! didn't know that ASCII values were different! when you say platform is the OS you talking or compiler?

If OS, my programs worked on both linux and windows, and if compiler my program compiles on VS and g++.
Nov 24 '08 #12
JosAH
11,448 Expert 8TB
Waidaminnit: ASCII is ASCII is ASCII. In ASCII a capital A has the code 65, everywhere. The C or C++ language (or any other language) doesn't force you to remember those silly codes, simply use 'A' and the language does the rest. This isn't the 1960s anymore.

Those languages can do that on non-ASCII machines as well, e.g. on an IBM EBCDIC machine the notation 'A' results in the correct EBCDIC code for a capital A. Dumb numbers can't do that.

kind regards,

Jos
Nov 24 '08 #13
donbock
2,422 Expert 2GB
@boxfish
Waidaminnit: ASCII is ASCII is ASCII.
I'm sure what boxfish meant was the the encoding of characters may be different on different platforms. Some might use ASCII, some might use EBCDIC, some might use other encoding systems. That's one reason why character constants are better than bare numbers.

Another reason is that bare numbers (so called magic numbers) should be avoided because they poorly document/explain what the code is trying to do.
Of course, this objection could be met by a collection of macro definitions such as this [tongue firmly in cheek]:
#define SMALL_A 65

However, if you're totally paranoid about portable code I don't think you can even take for granted that the range of character codes between 'a' and 'z' are exclusively lower case letters. Perhaps there are holes, perhaps they are out of order.

By far the best way to go is to use the functions in ctype.h (suggested by boxfish).
Nov 24 '08 #14
You need use ctype.h for remove space,punctuations and another grammatical concepts

See following palindrome checker

Expand|Select|Wrap|Line Numbers
  1. /*Write a program of palindrome using stack [implement stack using array]:
  2. Note: Punctuation, Capitalization, and Spaces are ignored. For Example- Poor dan is in a droop.
  3.  
  4. Date : February 21,2014 */
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10.  
  11. bool is_palindrome(const char* s)
  12. {
  13.     int i = 0;
  14.     int j = strlen(s) - 1;
  15.     while(j >= 0)
  16.     {
  17.         if(!isalpha(s[i]))
  18.         {
  19.             ++i;
  20.         }
  21.         else if(!isalpha(s[j]))
  22.         {
  23.             --j;
  24.         }
  25.         else if(tolower(s[i]) != tolower(s[j]))
  26.         {
  27.             return false;
  28.         }
  29.         ++i;
  30.         --j;
  31.     }
  32.     return true;
  33. }
  34.  
  35.  
  36. void printme(const char* s)
  37. {
  38.     printf(" \" %s\" ",s);
  39.     if(is_palindrome(s))
  40.     {
  41.         printf(" IS a palindrome! \n");
  42.     }
  43.     else
  44.     {
  45.         printf(" IS NOT a palindrome! \n");
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51.     char s[] = "kajak";
  52.     char s2[] = "Poor Dan is in a droop";
  53.     char s3[] = "not a palindrome";
  54.  
  55.     printme(s);
  56.     printme(s2);
  57.     printme(s3);
  58.     return 0;
  59. }
Mar 1 '14 #15
Expand|Select|Wrap|Line Numbers
  1. /*Write a program of palindrome using stack [implement stack using array]:
  2. Note: Punctuation, Capitalization, and Spaces are ignored. For Example- Poor dan is in a droop.
  3.  
  4. Date : February 21,2014 */
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10.  
  11. bool is_palindrome(const char* s)
  12. {
  13.     int i = 0;
  14.     int j = strlen(s) - 1;
  15.     while(j >= 0)
  16.     {
  17.         if(!isalpha(s[i]))
  18.         {
  19.             ++i;
  20.         }
  21.         else if(!isalpha(s[j]))
  22.         {
  23.             --j;
  24.         }
  25.         else if(tolower(s[i]) != tolower(s[j]))
  26.         {
  27.             return false;
  28.         }
  29.         ++i;
  30.         --j;
  31.     }
  32.     return true;
  33. }
  34.  
  35.  
  36. void printme(const char* s)
  37. {
  38.     printf(" \" %s\" ",s);
  39.     if(is_palindrome(s))
  40.     {
  41.         printf(" IS a palindrome! \n");
  42.     }
  43.     else
  44.     {
  45.         printf(" IS NOT a palindrome! \n");
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51.     char s[] = "kajak";
  52.     char s2[] = "Poor Dan is in a droop";
  53.     char s3[] = "not a palindrome";
  54.  
  55.     printme(s);
  56.     printme(s2);
  57.     printme(s3);
  58.     return 0;
  59. }
  60.  
Mar 1 '14 #16

Post your reply

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

Similar topics

7 posts views Thread by Lachlan Hunt | last post: by
reply views Thread by Chris Leffer | last post: by
6 posts views Thread by Tashfeen Bhimdi | last post: by
5 posts views Thread by joe | last post: by
4 posts views Thread by kdsutaia | last post: by
10 posts views Thread by Mike Copeland | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.