473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
5 3406
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are...
2
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers...
4
by: arnuld | last post by:
as i said, i have restarted the book because i overlooked some material. i want to have some comments/views on this solution. it runs fine, BTW. ------------------ PROGRAMME -------------- /*...
16
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before...
1
by: arnuld | last post by:
this is "word counting" example programme from K&R2 section 1.5.4. it runs without any error/warning but does not work properly, i.e. the OUTPUT 1 should be: NEWLINES: 1 WORDS: 1 CHARs: 5
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
4
by: arnuld | last post by:
/* K&R@, 1.5.1 - File Copying */ #include<stdio.h> int main() { int c; while((c = getchar())!= EOF) putchar(c);
6
by: arnuld | last post by:
This is the example from section 4.2, page 71 of K&R2: double atof( char s ) { int i, sign; double val, power; for( i = 0; isspace( s ); ++i )
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.