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

A

arnuld

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 != '\0'; ++i)
{
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1 != s2[k])
s1[j++] = s1;
}
}

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

Papastefanos Serafeim

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 != '\0'; ++i) {
ok=1;
for(k = 0; s2[k] != '\0'; ++k) {
if(s1 == s2[k]) ok = 0;
}
if(ok==1) s1[j++] = s1;
}
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...
 
A

arnuld

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 != '\0'; ++i) {
ok=1;
for(k = 0; s2[k] != '\0'; ++k) {
if(s1 == s2[k]) ok = 0;
}
if(ok==1) s1[j++] = s1;
}
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 ?
 
R

Rajesh S R

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 != '\0'; ++i)
{
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1 != s2[k])
s1[j++] = s1;
}
}

s1[j] = '\0';


I am not able to get what this code is doing.

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

p_cricket_guy

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:

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

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


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;" 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 != '\0'; ++i)
{
present = 0;
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1 == s2[k]) {
present = 1;
break;
}
}
if (!present)
s1[j++] = s1;
}

s1[j] = '\0';
}


p_c_g
 
B

Barry Schwarz

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 != '\0'; ++i)
{
for(k = 0; s2[k] != '\0'; ++k)
{
if(s1 != 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;


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top