A function to remove characters in a string from another string - c

I am in an introductory computer programming class and this is the last question on the assignment and any help would be appreciated. I know how to remove a character from a string, but I can't figure out how to take characters from one string and remove them from another string. The call to the function looks like this:
void removeChars(char *str, const char *cs)
Any help would be appreciated. I am using C by the way.

If you know how to remove a single character from a string, then removing all of the characters in one string from another is just as easy. You just need to re-use your first function but call it for each character in string to be removed.

You need to use substring search library function - "strstr" should work for you

Related

Splitting input lines at a comma

I am reading the contents of a file into a 2D array. The file is of the type:
FirstName,Surname
FirstName,Surname
etc. This is a homework exercise, and we can assume that everyone has a first name and a surname.
How would I go about splitting the line using the comma so that in a 2D array it would look like this:
char name[100][2];
with
Column1 Column2
Row 0 FirstName Surname
Row 1 FirstName Surname
I am really struggling with this and couldn't find any help that I could understand.
You can use strtok to tokenize your string based on a delimiter, and then strcpy the pointer to the token returned into your name array.
Alternatively, you could use strchr to find the location of the comma, and then use memcpy to copy the parts of the string before and after this point into your name array. This way will also preserve your initial string and not mangle it the way strtok would. It'll also be more thread-safe than using strtok.
Note: a thread-safe alternative to strtok is strtok_r, however that's declared as part of the POSIX standard. If that function's not available to you there may be a similar one defined for your environment.
EDIT: Another way is by using sscanf, however you won't be able to use the %s format specifier for the first string, you'd instead have to use a specifier with a set of characters to not match against (','). Since it's homework (and really simply) I'll let you figure that out.
EDIT2: Also, your array should be char name[2][100] for an array of two strings, each of 100 chars in size. Otherwise, with the way you have it, you'll have an array of 100 strings, each of 2 chars in size.

iterating string in C, word by word

I just started learning C. What I am trying to right now is that I have two strings in which each word is separated by white spaces and I have to return the number of matching words in both strings. So, is there any function in C where I can take each word and compare it to everyother word in another string, if not any idea on how I can do that.
Break up the first string in words, this you can do in any number of ways everything from looping through the character array inserting \0 at each space to using strtok.
For each word found, go through the other string using strstr which checks if a string is in there. just check return value from strstr, if != NULL it found it.
I'd not use strtok but stick with pointer arithmetics length comparison and memcmp to compare strings of equal length.
There are two problems here:
1) splitting each string into words
The strtok() function can split a string into words.
It is a meaningful exercise to imagine how you might write your own equivalent to strtok.
The rosetta project shows both a strtok and a custom method approach to precisely this problem.
I would naturally write my own parser, as its the kind of code that appeals to me. It could be a fun exercise for you.
2) finding those words in one string that are also in another
If you iterate over each word in one string for each word in another, it has O(n*n) complexity.
If you index the words in one string it will take just O(n) which is substantially quicker (if your input is large enough to make this interesting). It is worth imagining how you might build a hashtable of the words in one string so that you can look for the words in the other.

Editing a string (char array) copying to input string (scanf or fgets.. gets) possible?

I have a question for you, is it possible to the the following in C?
I mean:
In the code I would have something like
char example[] = "Single"
then do edit this string, I would copy to input of scanf,
and then I could just use backspace to delete the last char 'e',
then I would press enter, and scanf would store the new example string
on the same pointer example..
Possible in C ?
From what I understand you want to interactively edit string on your terminal. Functions from stdio.h don't offer such functionality. They only read and write data.
You can use libraries such as readline or ncurses to achieve this.
You can't use example as a place to store data. Think of this as a constant.
Instead you can say
char example[7] = "Single";
Now you can change those 6 characters if you like but anything bigger will be a problem.

C: Search and Replace/Delete

Is there a function in C that lets you search for a particular string and delete/replace it? If not, how do I do it myself?
It can be dangerous to do a search and replace - unless you are just replacing single chars with another char (ie change all the 'a' to 'b'). Reason being the replaced value could try and make the char array longer. Better to copy the string and replace as you into a new char array that can hold the result. A good find C function in strstr(). So you can find you string - copy everything before it to another buffer, add your replacement to the buffer - and repeat.
<string.h> is full of string processing functions. Look here for reference:
http://www.edcc.edu/faculty/paul.bladek/c_string_functions.htm
http://www.cs.cf.ac.uk/Dave/C/node19.html

how can i parse a string using c

Given below is my string
char test[1000]="$GPGSA,A,3,14,20,22,25,31,32,,,,,,,2.4,1.4,1.9*3A
$GPGSV,4,1,16,31,76,060,35,14,28,070,34,20,32,309,32,32,61,309,32*72\n
$GPGSV,4,2,16,25,21,053,29,24,37,258,29,23,14,277,27,12,,,21*44\n
$GPGSV,4,3,16,22,13,133,20,11,20,272,,16,11,161,,30,,,*4F\
n$GPGSV,4,4,16,29,,,,28,,,,27,,,,26,,,*7E\n
$GPGGA,150427.8,4001.022852,N,10505.269674,W,1,06,1.4,1559.6,M,-21.0,M,,*53\n
$PQXFI,150427.8,4001.022852,N,10505.269674,W,1559.6,35.12,25.46,2.05*4A\n
$GPVTG,nan,T,nan,M,0.0,N,0.0,K,A*23\n
$GPRMC,150427.8,A,4001.022852,N,10505.269674,W,0.0,,280611,,,A*50";
I want to get string
"$GPGGA,150427.8,4001.022852,N,10505.269674,W,1,06,1.4,1559.6,M,-21.0,M,,*53\n"
from above big string using C Language.
Please help me out.
You say which line you want, but you didn't say why. If you say what it is about this line that makes it the line you are after, then I could comment on how you'd find it.
But basically, you'll probably want to separate the string into lines. You can use strtok() to break on \n. You can then examine the lines, one at a time.
This looks like GPS data to me and is used (and parsed) in many applications.
http://mbed.org/users/todotani/notebook/gps-nmea-parser/
http://www.edaboard.com/thread204021.html
You might be able to save yourself some time by re-using some other open source parsers.
The strstr() command, part of the standard C library, can be used to find a substring within a string.

Resources