Function for removing character from a word

This is a discussion on Function for removing character from a word within the C Programming forums, part of the General Programming Boards category; Is there any function in C to remove a character from a word ?...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    20

    Function for removing character from a word

    Is there any function in C to remove a character from a word ?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by TheDeveloper View Post
    Is there any function in C to remove a character from a word ?
    Use strtok().
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by BEN10 View Post
    Use strtok().
    Thanks a lot for your reply but my case is different here

    I will have " and " in the start and end of the string.

    for example :
    Input : "Word"
    Output : Word

    I need to remove these " and " and extract the word alone.

    Is there any particular function for it ?

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Use strtok(), it's not a bad idea!...Take a look at the manual.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd use a while loop with strchr().

    Code:
    char string[] = { "I like  this  string" };   //note the double spaces around the word this
    char *ptr, ch = '"'; //a double quote surrounded by single quotation marks
    int where, i, j;
    
    string[7] = '"';  //now we add the double quotation marks
    string[12] = '"';
    
    while (ptr = strchr(string, ch) ) {
       //print where it is
       where = ptr - string;
       printf("\n Double quotation marks were found at %d", where);
       
       //slide all the chars after the location of where, to the left by one char   
       for(i = where, j = strlen(string); i < j; i++)
          string[i] = string[i + 1];
    }
    I haven't run the above code, but it should be close if not perfect. You'll want to include string.h, of course.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    20
    Quote Originally Posted by flexo87 View Post
    Use strtok(), it's not a bad idea!...Take a look at the manual.
    Yes it works, thanks a lot.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    I'd use a while loop with strchr().

    [code]
    char string[] = { "I like this string" }; //note the double spaces around the word this
    char *ptr, ch = '"'; //a double quote surrounded by single quotation marks
    int where, i, j;

    string[7] = '"'; //now we add the double quotation marks
    string[12] = '"';
    That's a crazy ass way to do it.
    Code:
    char string[] = "I like \"this\" string";
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21