GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Nov-2008, 05:57
wedel wedel is offline
New Member
 
Join Date: Jun 2007
Posts: 20
wedel is on a distinguished road

Deleting a character from a string using getopt() function


Hello
i have a program of deleting a character from a string using getopt() function, meaning that i have to enter an option with an argument (this is the character that had to be deleted from the string). i'm not asking for the code i'm just asking for the method. how should i do it. actually i did the search of the character in the given string and it works. i just need to know how to do the rest.
Thankyou
  #2  
Old 20-Nov-2008, 09:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: unix system programming problem


Quote:
Originally Posted by wedel
...asking for the method.
I thought you had already decided to use getopt(). If you have a system with getopt, then try "man getopt"

If you want an example of use of getopt:

CPP / C++ / C Code:
/* 
 * Test of getopt with simple options
 *
 * davekw7x
 */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <ctype.h>

int main(int argc, char **argv)
{
    int aflag = 0;
    int bflag = 0;
    char *cvalue = NULL;
    int index;
    int c;

    opterr = 0;

    while ((c = getopt(argc, argv, "abc:")) != -1)
	switch (c) {
            case 'a':
                aflag = 1;
                break;
            case 'b':
                bflag = 1;
                break;
            case 'c':
                cvalue = optarg;
                break;
            case '?':
                if (isprint(optopt))
                    fprintf(stderr, "Unknown option: -%c.\n", optopt);
                else
                    fprintf(stderr,
                            "Unknown option character: 0x%02x.\n", optopt);
                return EXIT_FAILURE;
            default:
                abort();
	}

    printf("aflag = %d, bflag = %d", aflag, bflag);
    if (!cvalue) {
        printf(", cvalue = (NULL)\n");
    }
    else {
        printf(", cvalue = <%s>", cvalue);
        printf(", cvalue[0] = '%c'\n", cvalue[0]);
    }




    for (index = optind; index < argc; index++) {
	printf("Non-option argument %s\n", argv[index]);
    }
    return EXIT_SUCCESS;
}

Some runs on my Linux system (GNU gcc version 4.1.2). Same results on Windows XP/cygwin with GNU gcc version 3.4.4.

Code:
$./testopt aflag = 0, bflag = 0, cvalue = (NULL)
Code:
$ ./testopt -a aflag = 1, bflag = 0, cvalue = (NULL)

Code:
$ ./testopt -cthisisatest huh? aflag = 0, bflag = 0, cvalue = <thisisatest>, cvalue[0] = 't' Non-option argument huh?

Of course, you can have any combinations of arguments in any order. Note also that you can have a space between the '-c' and the argument. Or not. That's the kind of stuff that getopt() brings to the party. Of course, getopt() is not a C standard library function and some vendors (Borland, Microsoft) don't supply it, but all GNU compiler distributions seem to have it. (It is one of those legacy UNIX functions that have been there forever even if it is not part of the standard C library...)

Regards,

Dave

Footnote: Unlike my treatment of non-option arguments, I didn't bother to test any '-c' arguments for non-printing chars. Try it if you really want to.
Last edited by davekw7x : 20-Nov-2008 at 10:46.
  #3  
Old 20-Nov-2008, 14:23
wedel wedel is offline
New Member
 
Join Date: Jun 2007
Posts: 20
wedel is on a distinguished road

Re: Deleting a character from a string using getopt() function


Thank you for replying to question. actually i have already used getopt() and i have no problem with it. my problem is how to delete a character from a given string.
thanks again. your example was useful.
  #4  
Old 20-Nov-2008, 14:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Deleting a character from a string using getopt() function


Quote:
Originally Posted by wedel
...how to delete a character from a given string...

Brute force:

1. Find the index of the character you want to delete, say indx
2. Make a loop using a variable, say i, that starts at i = indx and sets str[ i ] = str [ i+1 ] and then increments i
3. Exit the loop after you have copied the terminating zero byte

There are other ways. (Using memmove, for example.)

Regards,

Dave
  #5  
Old 22-Nov-2008, 06:19
wedel wedel is offline
New Member
 
Join Date: Jun 2007
Posts: 20
wedel is on a distinguished road

Re: Deleting a character from a string using getopt() function


Thanks again for replying to my question. I'm sorry for not being clear it's my fault . it's deleting all the occurences of the mentioned character .what i wanna do is somthing like:
Code:
./delete -d l hello
the result will be:
Code:
heo
  #6  
Old 22-Nov-2008, 08:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Deleting a character from a string using getopt() function


Quote:
Originally Posted by wedel
...deleting all the occurences of the mentioned character .
You are looking for needles in a haystack. You will look at every piece of straw in the haystack. If that piece of straw is not a needle, then transfer that piece of straw to another haystack, the "result."

One program approach for a generic problem like this:
  1. Suppose the string to be searched (including a terminating zero byte) is in array "haystack"
  2. Suppose the character to be deleted is the value f a char, "needle"
  3. Define an array, "result", that is at least as large as the "haystack" array (including the terminating zero byte).
  4. Define two ints, i, and j; set them both to zero. The variable i is the index of the next char to be written in the result; the variable j is the index of the next character to be retrieved from the haystack.

  5. Execute the following until loop you have transferred the zero byte from the haystack to the result:
    Code:
    BEGIN LOOP IF the current piece of straw from the haystack is not a needle,THEN Transfer the straw to the target Increment i END IF increment j END LOOP

Now, instead of making another array, if you want to keep the same haystack but without the needles, the above approach works just fine.

The loop looks the same: Use two index variables. Look at each haystack element with j and store non-needle elements back in the haystack array using i.


Regards,

Dave
  #7  
Old 23-Nov-2008, 05:23
wedel wedel is offline
New Member
 
Join Date: Jun 2007
Posts: 20
wedel is on a distinguished road

Re: Deleting a character from a string using getopt() function


Tanks Dave. I will try this out and i'll show the code. but i have a question: do I have to use strchr() to search the character.
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Probing for TCP connections kterry C Programming Language 3 14-Feb-2008 12:32
C++ object oriented programming & file processing mrdell_06 MS Visual C++ / MFC Forum 2 04-Oct-2006 12:02
problem with system() and win32 shell lordfuoco C++ Forum 0 27-Jun-2006 04:09
RAW File System on a CD Problem SemperFi Computer Software Forum - Windows 13 03-Sep-2004 07:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:29.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.