remove punctuation in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kpdp
    New Member
    • Nov 2008
    • 7

    remove punctuation in C

    hey,
    can anyone help me write up a function to remove space, semicolons, etc?
    ty.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Do you have any suggestions for how you might recognize punctuation or space characters in a string?

    Comment

    • kpdp
      New Member
      • Nov 2008
      • 7

      #3
      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.

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        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?

        Comment

        • kpdp
          New Member
          • Nov 2008
          • 7

          #5
          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 "mynameispe ter" ?
          How do i use the function?

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by kpdp
            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 "mynameispe ter" ?
            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.

            Comment

            • oler1s
              Recognized Expert Contributor
              • Aug 2007
              • 671

              #7
              My question is that how do i write something like "My name is Peter" and I get back "mynameispe ter" ?
              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.

              Comment

              • drjay1627
                New Member
                • Nov 2008
                • 19

                #8
                Originally posted by kpdp
                hey,
                can anyone help me write up a function to remove space, semicolons, etc?
                ty.
                I haven't done this in C but have done this in C++, java and C#.

                psuedo code:

                string someFunction(st ring 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

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by drjay1627
                  by for string manupilations ascii values are the way to go!
                  No they are not.

                  kind regards,

                  Jos

                  Comment

                  • drjay1627
                    New Member
                    • Nov 2008
                    • 19

                    #10
                    Originally posted by JosAH
                    No they are not.

                    kind regards,

                    Jos
                    why not??? what do you suggest?

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      Originally posted by drjay1627
                      why not??? what do you suggest?
                      Because the ASCII values of characters may be different on different platforms. It is safer to use the actual letters, like this:
                      Code:
                      char j = string[i];
                      if((j >= 'a' && j <= 'z') || (j >= 'A' && j <= 'Z') || j == ' ' || (j >= '0' && j <= '9')) {
                          str += string[i];
                      }
                      But the best thing to do is use isalpha() and friends. I think they're in stdlib.h, or cstdlib.
                      Code:
                      char j = string[i];
                      if(isalpha(j) || isspace(j) || isdigit(j)) {
                          str += string[i];
                      }
                      islower() and isupper() may be useful too.
                      Hope this helps.
                      Edit:
                      Also check out ispunct().

                      Comment

                      • drjay1627
                        New Member
                        • Nov 2008
                        • 19

                        #12
                        Originally posted by boxfish
                        Because the ASCII values of characters may be different on different platforms. It is safer to use the actual letters, like this:
                        Code:
                        char j = string[i];
                        if((j >= 'a' && j <= 'z') || (j >= 'A' && j <= 'Z') || j == ' ' || (j >= '0' && j <= '9')) {
                            str += string[i];
                        }
                        But the best thing to do is use isalpha() and friends. I think they're in stdlib.h, or cstdlib.
                        Code:
                        char j = string[i];
                        if(isalpha(j) || isspace(j) || isdigit(j)) {
                            str += string[i];
                        }
                        islower() and isupper() may be useful too.
                        Hope this helps.
                        Edit:
                        Also check out ispunct().
                        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++.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          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

                          Comment

                          • donbock
                            Recognized Expert Top Contributor
                            • Mar 2008
                            • 2427

                            #14
                            Originally posted by boxfish
                            Because the ASCII values of characters may be different on different platforms...
                            Originally posted by JosAH
                            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).

                            Comment

                            • siddharth29
                              New Member
                              • Mar 2014
                              • 2

                              #15
                              You need use ctype.h for remove space,punctuati ons and another grammatical concepts

                              See following palindrome checker

                              Code:
                              /*Write a program of palindrome using stack [implement stack using array]:
                              Note: Punctuation, Capitalization, and Spaces are ignored. For Example- Poor dan is in a droop.
                              
                              Date : February 21,2014 */
                              
                              #include <stdio.h>
                              #include <ctype.h>
                              #include <string.h>
                              #include <stdbool.h>
                              
                              bool is_palindrome(const char* s)
                              {
                                  int i = 0;
                                  int j = strlen(s) - 1;
                                  while(j >= 0)
                                  {
                                      if(!isalpha(s[i]))
                                      {
                                          ++i;
                                      }
                                      else if(!isalpha(s[j]))
                                      {
                                          --j;
                                      }
                                      else if(tolower(s[i]) != tolower(s[j]))
                                      {
                                          return false;
                                      }
                                      ++i;
                                      --j;
                                  }
                                  return true;
                              }
                              
                              
                              void printme(const char* s)
                              {
                                  printf(" \" %s\" ",s);
                                  if(is_palindrome(s))
                                  {
                                      printf(" IS a palindrome! \n");
                                  }
                                  else
                                  {
                                      printf(" IS NOT a palindrome! \n");
                                  }
                              }
                              
                              int main()
                              {
                                  char s[] = "kajak";
                                  char s2[] = "Poor Dan is in a droop";
                                  char s3[] = "not a palindrome";
                              
                                  printme(s);
                                  printme(s2);
                                  printme(s3);
                                  return 0;
                              }
                              Last edited by Rabbit; Mar 1 '14, 06:33 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

                              Comment

                              Working...