By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

K&R2 section 2.8 (exercise 2.4) "squeeze"

P: n/a
it compiles without any trouble but produces "Segmentation Fault" when
i try to run it. since i am at chapter 2 so my knowledge of arrays is
limited to chapter 1:

------------------------- PROGRAMME -----------------------------
/* K&R2 section 2.8

Increment and Decrement Operators
STATEMENT: write an alternative version of squeeze(s1, s2) that
deletes each character
in s1 that matches any character in the string s2.

*/

#include <stdio.h>

void squeeze(char s1[], char s2[]);
int main(void) {
char arr1[] = "like this and this";
char arr2[] = "wxyzspqw";

squeeze(arr1, arr2);

printf("array1: %s\narray2: %s\n\nmodified_array: %s\n", arr1, arr2,
arr1);

return 0;

}
void squeeze(char s1[], char s2[])
{
int i, j, k;

for(i = j = 0; s1[i] != '\0'; ++i)
{
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1[i] != s2[k])
s1[j++] = s1[i];
}
}

s1[j] = '\0';
}

Mar 13 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Try this modified version of your function instead

void squeeze(char s1[], char s2[]) {
int i, j, k;
int ok = 0;
for(i = j = 0; s1[i] != '\0'; ++i) {
ok=1;
for(k = 0; s2[k] != '\0'; ++k) {
if(s1[i] == s2[k]) ok = 0;
}
if(ok==1) s1[j++] = s1[i];
}
s1[j] = '\0';
}

The ok variable is a flag which will be true if the current
character does not belong to those which have to be
removed...

--
Papastefanos Serafeim
se******@otenet.gr
? "arnuld" <ge*********@gmail.com?????? ??? ??????
news:11********************@v33g2000cwv.googlegrou ps.com...
it compiles without any trouble but produces "Segmentation Fault" when
i try to run it. since i am at chapter 2 so my knowledge of arrays is
limited to chapter 1:

------------------------- PROGRAMME -----------------------------
/* K&R2 section 2.8

Increment and Decrement Operators
STATEMENT: write an alternative version of squeeze(s1, s2) that
deletes each character
in s1 that matches any character in the string s2.

*/

#include <stdio.h>

void squeeze(char s1[], char s2[]);
int main(void) {
char arr1[] = "like this and this";
char arr2[] = "wxyzspqw";

squeeze(arr1, arr2);

printf("array1: %s\narray2: %s\n\nmodified_array: %s\n", arr1, arr2,
arr1);

return 0;

}
void squeeze(char s1[], char s2[])
{
int i, j, k;

for(i = j = 0; s1[i] != '\0'; ++i)
{
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1[i] != s2[k])
s1[j++] = s1[i];
}
}

s1[j] = '\0';
}

Mar 13 '07 #2

P: n/a
On Mar 13, 8:04 pm, "Papastefanos Serafeim" <seraf...@otenet.grwrote:
Try this modified version of your function instead

void squeeze(char s1[], char s2[]) {
int i, j, k;
int ok = 0;
for(i = j = 0; s1[i] != '\0'; ++i) {
ok=1;
for(k = 0; s2[k] != '\0'; ++k) {
if(s1[i] == s2[k]) ok = 0;
}
if(ok==1) s1[j++] = s1[i];
}
s1[j] = '\0';

}

The ok variable is a flag which will be true if the current
character does not belong to those which have to be
removed...
Thanks

can you tell me what my code was doing wrong and at what place ?

Mar 13 '07 #3

P: n/a
On Mar 13, 7:28 pm, "arnuld" <geek.arn...@gmail.comwrote:
it compiles without any trouble but produces "Segmentation Fault" when
i try to run it. since i am at chapter 2 so my knowledge of arrays is
limited to chapter 1:

------------------------- PROGRAMME -----------------------------
/* K&R2 section 2.8

Increment and Decrement Operators

STATEMENT: write an alternative version of squeeze(s1, s2) that
deletes each character
in s1 that matches any character in the string s2.

*/

#include <stdio.h>

void squeeze(char s1[], char s2[]);

int main(void) {
char arr1[] = "like this and this";
char arr2[] = "wxyzspqw";

squeeze(arr1, arr2);

printf("array1: %s\narray2: %s\n\nmodified_array: %s\n", arr1, arr2,
arr1);

return 0;

}

void squeeze(char s1[], char s2[])
{
int i, j, k;

for(i = j = 0; s1[i] != '\0'; ++i)
{
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1[i] != s2[k])
s1[j++] = s1[i];
}
}

s1[j] = '\0';
I am not able to get what this code is doing.

But still, I think that:
s1[j++] = s1[i];
this statement is the culprit, You don't check if s1[j] == '\0' after
this statement!
I think this will solve your problem.
>
}- Hide quoted text -

- Show quoted text -

Mar 13 '07 #4

P: n/a
On Mar 13, 7:28 pm, "arnuld" <geek.arn...@gmail.comwrote:
it compiles without any trouble but produces "Segmentation Fault" when
i try to run it. since i am at chapter 2 so my knowledge of arrays is
limited to chapter 1:
<--snipped-->
void squeeze(char s1[], char s2[])
{
int i, j, k;

for(i = j = 0; s1[i] != '\0'; ++i)
{
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1[i] != s2[k])
s1[j++] = s1[i];
Your problem is here. You are incrementing j for every
character in s2 that does not match a character in s1.
When you are processing first character in s1, i.e 'l', you
copy 'l' 8 times and increment j 8 times since 'l' does not
match any char in "wxyzspqw". Eventually you overflow s1.

In the inner loop, you can set a flag that tells if any of
characters in s2 matched the current character in s1. Move
"s1[j++] = s1[i];" to the outer loop under that condition that
the flag is not set. Ofcourse there are other ways of fixing it.

Here is the squeeze function with the above changes:
void squeeze(char s1[], char s2[])
{
int i, j, k, present;

for(i = j = 0; s1[i] != '\0'; ++i)
{
present = 0;
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1[i] == s2[k]) {
present = 1;
break;
}
}
if (!present)
s1[j++] = s1[i];
}

s1[j] = '\0';
}
p_c_g

Mar 13 '07 #5

P: n/a
On 13 Mar 2007 07:28:58 -0700, "arnuld" <ge*********@gmail.comwrote:
>it compiles without any trouble but produces "Segmentation Fault" when
i try to run it. since i am at chapter 2 so my knowledge of arrays is
limited to chapter 1:

------------------------- PROGRAMME -----------------------------
/* K&R2 section 2.8

Increment and Decrement Operators
STATEMENT: write an alternative version of squeeze(s1, s2) that
deletes each character
in s1 that matches any character in the string s2.

*/

#include <stdio.h>

void squeeze(char s1[], char s2[]);
int main(void) {
char arr1[] = "like this and this";
char arr2[] = "wxyzspqw";

squeeze(arr1, arr2);

printf("array1: %s\narray2: %s\n\nmodified_array: %s\n", arr1, arr2,
arr1);
You print arr1 twice. What makes you think it will any different the
second time?
>
return 0;

}
void squeeze(char s1[], char s2[])
{
int i, j, k;

for(i = j = 0; s1[i] != '\0'; ++i)
{
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1[i] != s2[k])
At this point, all you have determined is that s1[0] is not equal to
s2[0].

What happens if s1[0] is different from s2[0] and different from
s2[1]? How many times will you copy s1[0] to s1[j]? What happens to
the original character at s1[1]?
> s1[j++] = s1[i];
It is way to soon to determin that s1[0] is not equal to any s2[k]
which is what you need to decide to keep it in the final string.

You update s1 in place. Therefore, you can never print the original
string.
> }
}

s1[j] = '\0';
}
You really should take a piece of scrap paper and pretend you are the
computer. Execute each statement and keep track of the results. It
is called desk checking.
Remove del for email
Mar 14 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.