Archived

This topic is now archived and is closed to further replies.

White Crow

string function problem

Recommended Posts

Hi I am stuck on an exercise in the book: The C programming language. There is the source the function squeeze and I have to rewrite the function so, squeeze(s1, s2), where I have to delete each character in s1 that occures in s2. I tried something like: for(i = j =0; s1 != s2[j] && s1[i] != '\0'; ++j) { if (s2[j] == '\0') \\if the end of the string is encountered { j = 0; \\go to the beginning of the string ++i;\\ go to the next character in the other string } } if (s1[i] == s2[j]) s1[i] = '\0'; after that I try to print the string s1 in main but nothing is outputted to the screen. And the problem has to be in the function. Could someone point out the problem? Oh yeah, one more question. If I think a know the subject which is explained in the book (by example arrays), but I cannot make an exercise in that book because I cannot figure out a solution, is that a sign that I don't know the subject well enough or another reason? There were some errors in the source that I fixed. And nothing has to be done to s1 if no characters of s2 matches those of s1. If some of s2 are the same in s1, they have to be deleted in s1. Oh yeah and s1 does contain characters in main() [edited by - White Crow on August 6, 2003 5:18:56 PM] [edited by - White Crow on August 6, 2003 5:20:32 PM]

Share this post


Link to post
Share on other sites
Are you trying to output s1? From what I can tell, you don''t change a thing in s1, s2 or s, so it should at least output something (if there''s something in the s1 array). And if your writing s to the screen, well, it''s empty (at least in the source I see).

And I''d read the chapters you need - to do the exercises - again. It saves a lot of ''Ooo, why doesn''t this work?!'' later on

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
s1 and s2 are NULL terminated strings

for(i = 0; s2 != ''\0''; ++i)
{
for(j = 0; s1[j] != ''\0''; ++j)
{
if(s1[j] == s2[i])
s1[j] = '' '';//set the character in s1 to a space
}
}
it''s easier to read if you use 2 loops..


this should work...good luck =)

Share this post


Link to post
Share on other sites
Does your solution just compare s1[1] with s2[1] etc? It has to compare each character with each character not only those who are in the same position of the both strings.

Maybe the original exercise will make things clear:

write a function squeeze(s1, s2) that deletes each character in s1 that matches any character with in the string s2.

Share this post


Link to post
Share on other sites
quote:
Original post by White Crow
Does your solution just compare s1[1] with s2[1] etc? It has to compare each character with each character not only those who are in the same position of the both strings.

Maybe the original exercise will make things clear:

write a function squeeze(s1, s2) that deletes each character in s1 that matches any character with in the string s2.

It is correct.
And next time please use [ source ] *code here* [ / source ]
Your code didn't look right in the post (s1 != s2[ j ] instead of s1[ i ] != s2[ j ]).

So the correct one is:

for(i = 0; s2[i] != '\0'; ++i)
{
for(j = 0; s1[j] != '\0'; ++j)
{
if(s1[j] == s2[i])
s1[j] = ' ';//set the character in s1 to a space

}
}




[edited by - EL on August 6, 2003 5:31:23 PM]

[edited by - EL on August 6, 2003 5:32:46 PM]

Share this post


Link to post
Share on other sites
Yeah this works! But i do not understand the code fully. When I see this code I only would say that the character in s1 will become a space when only the same character in s2 stands in the right position but that is not the case so could you explain why it works?

I mean how can it compare let's say s1[19] with s2[40] when j and i increment at the same time?

[edited by - White Crow on August 6, 2003 5:48:44 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by White Crow
Yeah this works! But i do not understand the code fully. When I see this code I only would say that the character in s1 will become a space when only the same character in s2 stands in the right position but that is not the case so could you explain why it works?

I mean how can it compare let's say s1[19] with s2[40] when j and i increment at the same time?

[edited by - White Crow on August 6, 2003 5:48:44 PM]


i and j don't increment at the same time. Analyse the code:
1. Enter first 'for': i = 0
2. enter second 'for': j = 0
3. do if test (and if it succeeds do s1[ j ] = ' ')
4. back to second 'for': j = 1
5. do if test (and if it succeeds do s1[ j ] = ' ')
6. back to second 'for': j = 2
7. do if test (and if it succeeds do s1[ j ] = ' ')
... and so on until s1[ j ] == '\0' and then and only then will i be incremented.



[edited by - EL on August 6, 2003 5:55:37 PM]

Share this post


Link to post
Share on other sites
To see if I got it

say s1[] = "gamedev" (j)
and s2[] = "development" (i)

first i = 0 then j = 0 then do if.
then back to the second for and j = 1 etc.

If "gamedev" is ended increment i to i=1 and check again the whole string "gamedev" if the "e" from development matches any character of string gamedev. And that until the string "development" is ended. Is that right? Although thanks for taking your time to help me.

Share this post


Link to post
Share on other sites