program help

Hello,

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

I`m not sure if I wrote this correct, and can`t test it due to my compiler won`t compile it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void squeeze (char s[], char p[])
{

  char *end = NULL;
  int i, j, v;
  
  for ( i = j = v = 0; s[i] != end; ++i)
      if (s[i] != p[j++])
          s[v++] = s[i];
  s[i] = end;
  
}
      

 
s[i] is of type char, your end (char *end = NULL;) is of type char*
remove the * at end and it should compile.
Last edited on
Hi,

I get now this error when trying to compile with gcc :

In function ‘main’:
Documents/program12.c:15: warning: too few arguments for format
Documents/program12.c:15: warning: too few arguments for format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <stdio.h>


void squeeze(char s[], char p[]);

int main()

{

   char *p = "Hello";
   char *p2 = "Yes";
   
   squeeze (p, p2);
   
   printf( "%s, p2");
   
   return 0;
}


void squeeze (char s[], char p[])
{

  
  int i, j, v;
  
  for ( i = j = v = 0; s[i] != 0; ++i)
      if (s[i] != p[j++])
          s[v++] = s[i];
  s[i] = 0;
  
}
       

 

@pacman169

try it this way..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>

using namespace std;

void squeeze(char s[], char p[]);

int main()

{

	char s[] = "Hello, student.";
	char p[] = "Yes.";

	squeeze(s, p);

	printf(s);

	return 0;
}


void squeeze(char s[], char p[])
{


	int i, j;
	for (i = 0; s[i] != '\0'; i++)
		for (j = 0; p[j] != '\0'; j++)
		{
			if (s[i] == p[j])
			{
				printf("%c is in both\n", s[i]);
			}
		}
}
your squeeze function works fine

yeah, try it this way.
The problem are these lines:

1
2
   char *p = "Hello";
   char *p2 = "Yes";

It's okey if you don't understand the reason:
in old c-code there was no keyword "const", today you would write
1
2
   const char *p = "Hello";
   const char *p2 = "Yes";

because you can't have a non-const pointer or lvalue-reference to an rvalue.
char* is treated different than other types so when compiling yours, this should give a warning because of deprecation. They can't take it out completely from C++ because it has to be backwards-compatible.
Last edited on
@whitenite1:

When I try to compile your code with gcc
warning: format not a string literal and no format arguments

@Gamer2015:

warning: passing argument 1 of ‘squeeze’ discards qualifiers from pointer target type
Last edited on
When I try to compile ...
1
2
3
4
printf( "%s, p2"); // warning: too few arguments for format

char s[] = "Hello, student.";
printf( s ); // warning: format not a string literal and no format arguments 

Yes, both cases do have something that is not quite what the printf() expects.

Note though that a warning is a mere warning, not an error. Your task, as one that knows the printf() and what your program should do, is to check the code and fix potential logical errors.
@pacman169
yeah, i know, that's why i said you should just make char s[]

if you really want to solve it with pointer you should make the 2nd parameter of the function a const char* (because you don't modify it) and you could dynamically allocate memory and then write "Hello" to p.
1
2
3
4
5
6
   char *p = new char[6]; // this one is modified
    strcpy(p, "Hello"); // incluide <cstring>
    const char *p2 = "Yes"; // this one is not modified


void squeeze(char* s, const char* p);


Note that you should use std::string for text
Last edited on
Ok here is my current program, which has at least one bug in it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <stdio.h>


void squeeze(char s[], char p[]);

int main()

{

   char p[] = "Hello";
   char p2[] = "Yes";
   
   squeeze (p, p2);
   
   
   printf("%s\n", p);
   
   return 0;
}


void squeeze (char s[], char p[])
{

  
  int i, j, v;
  
  for ( i = j = v = 0; s[i] != 0; ++i)
      if (s[i] != p[j++])
          s[v++] = s[i];
  s[i] = 0;
  
}




Output:

Hlloo


@Gamer2015: Please use the C-specific char *p = malloc(sizeof(char) * 6);

rather than the C++ specific construct.
Last edited on
@Gamer2015: Please use the C-specific char *p = malloc(sizeof(char) * 6);

Why the hell should I do that?
We are on a C++ Forum and you never mentioned you only want to use C.
Last edited on
I need to write a function squeeze(s1, s2) that deletes each character in s1 that matches any character in the string s2.

Your function does not do that.

Even worse, you do derefence out-of-range elements. You do increment the j on every iteration, but your input for p is shorter than input for s.

v = 0;
FOR EACH x in s
  IF p does not contain x
  THEN s[v++] = x;

s[v] = 0;
Topic archived. No new replies allowed.