Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing Character Array Content

Status
Not open for further replies.

TheTraveller

Programmer
Feb 10, 2002
3
US
Hello,

Disclaimer: I am trying to do this for a homework assignment, so please do not outright tell me how to code my solution.

I have been tasked by my instructor to write a function that compares string1 with string2. I am to take out of string1 whatever is not in string2.

Noting the various functions in the string.h library, I could not determine which one(s) I would need to use, if any.

I noted "strspn" as a good way to determine the first character in the "said" string that differs from the other, however I have noted that the "strtok" function seems to be my best bet, since I can continually search one string for "tokens" that are not in the other string.

Here is my problem. The strtok function returns a "pointer" to the first character in the token that differs from the other string. The function then places a NULL character immediately after the subscript that the pointer "points" to, replacing whatever character was in its place.

How can I utilize this pointer return value from the strtok function to my advantage? In other words, how can I use this pointer as a basis to then get the rest of the "token", if a NULL character is placed immediately after the subscript that the pointer points to?

Any feedback would be most appreciated. Thank you in advance.

The Traveller
 
hi,
I would declare three strings, two string pointers, one int counter and one int or boolean flag, outer loop through first string and inner loop through second string using pointers. utilise a flag for when character is present, if no flag in one iteration of outer loop then write character to third string using and incrementing the counter.
After final iteration of outer loop add null character to third string.

for(*sptr1 = string1; *sptr1, sptr1++)
{
for(*sptr2 = string2; *sptr2, sptr2++)
{
if(*sptr1 == *sptr2)
{
flag = 1;
break;
}
}
if(flag)
{
string3[i++]= *sptr1;
flag = 0;
}
}
string3='\0';

Hoping to get certified..in C programming.
 
Bigtamscot,

I appreciate your feedback. However, I wanted to utilize the string header's functions, in order to achieve my goal(s).

Is there anyone else out there that may know about this?

The Traveller
 
With strspn, you get a pointer to the first difference. Through clever use of strcpy and strspn you can achieve what it is that you need. I am being vague because you asked but if you want more info I will be more then happy to point you in the right direction.

Matt
 
Thank you all for your feedback. I was able to figure out how to do what I needed with the strtok function.

Once the assignment's due date has arrived, I will post my code on this forum, so that everyone can see how I solved this particular problem.

Thank you all for your feedback.

The Traveller
 
string a
string b
string c

get the first char of string a
and compare it to all char of string b
if the comparison goes completely through
the string b with no match do not add this
char to string c. Other wise add it to
string c at the first match and reiterate.
"do the loop again"
At the end only those char that match will
be in string c.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top