string function problem

Started by
7 comments, last by White Crow 20 years, 8 months ago
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 != '\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 == s2[j]) s1 = '\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, &#111;ne 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() </i> <SPAN CLASS=editedby>[edited by - White Crow &#111;n August 6, 2003 5:18:56 PM]</SPAN> <SPAN CLASS=editedby>[edited by - White Crow &#111;n August 6, 2003 5:20:32 PM]</SPAN>
~~~~~~I'll make you an offer you can't refuse...
Advertisement
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
el
s1 and s2 are NULL terminated strings

for(i = 0; s2 != ''\0''; ++i)
{
for(j = 0; s1[j] != ''\0''; ++j)
{
if(s1[j] == s2)<br> s1[j] = '' '';//set the character in s1 to a space<br> }<br>}<br>it''s easier to read if you use 2 loops.. <br><br><br>this should work…good luck =) </i>
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.
~~~~~~I'll make you an offer you can't refuse...
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 != s2[ j ]).<br><br>So the correct &#111;ne is:<br> <!–STARTSCRIPT–><pre class="source"><br><font color=blue>for</font>(i = 0; s2[<font color=purple>i</font>] != '\0'; ++i)<br>{<br><font color=blue>for</font>(j = 0; s1[<font color=purple>j</font>] != '\0'; ++j)<br>{<br><font color=blue>if</font>(s1[<font color=purple>j</font>] == s2[<font color=purple>i</font>])<br>s1[<font color=purple>j</font>] = ' ';<font color=gray>//set the character in s1 to a space<br></font><br>}<br>}<br></pre><!–ENDSCRIPT–><br><br> <br><br><SPAN CLASS=editedby>[edited by - EL &#111;n August 6, 2003 5:31:23 PM]</SPAN> <br><br><SPAN CLASS=editedby>[edited by - EL on August 6, 2003 5:32:46 PM]</SPAN>
el
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'll make you an offer you can't refuse...
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]
el
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.
~~~~~~I'll make you an offer you can't refuse...
That''s right! En graag gedaan
el

This topic is closed to new replies.

Advertisement