![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Deleting a character from a string using getopt() functionHello
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
|
|||
|
|||
Re: unix system programming problemQuote:
If you want an example of use of getopt: CPP / C++ / C Code:
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:
Code:
Code:
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
|
|||
|
|||
Re: Deleting a character from a string using getopt() functionThank 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
|
|||
|
|||
Re: Deleting a character from a string using getopt() functionQuote:
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
|
|||
|
|||
Re: Deleting a character from a string using getopt() functionThanks 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:
Code:
|
|
#6
|
|||
|
|||
Re: Deleting a character from a string using getopt() functionQuote:
One program approach for a generic problem like this:
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
|
|||
|
|||
Re: Deleting a character from a string using getopt() functionTanks 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 GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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