Hey, I have this simple lab that I'm working on, but I keep getting an error while compiling.

Here's the question:

**Write a program called lab09.c that
-prompts the user for a character and then a string.
-REMOVES all occurrences of that character from the string, and
-prints the changed string forwards and backwards.

The program must:
-use a macro (#define) to set MAX to 100 (maximum length of string is 99).
-define and use functions:
void squeeze( char c, char S[] ) ; /* removes all occurrences of c from S /
void printBackwards( char S[] ) ; /
prints string in reverse */
-exit or return with code 0 (zero)
-behave exactly like given program lab09

The program may:
-use gets if you wish

You may Assume:
-user always gives correctly formatted input.

Note:
-when you pass an array/string to a function, any changes the function
makes to that array/string are permanent.

Examples of running given lab09:

./lab09
Enter a character:p
Enter a string max 99 characters:Peter Piper picked a peck of pickled peppers!
FWD:Peter Pier icked a eck of ickled eers!:
BWD:!sree delkci fo kce a dekci reiP reteP:**

And here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100

char c;
char S[MAX];

void squeeze (char c, char S[]);
void printBackwards (char S[]);

int main (void)
{
        printf("Enter a character: ");
        scanf("%c", &c);
        printf ("Enter a string max 99 characters: ");
        scanf("%c", &S[MAX]);
        printf("\n");
        printf("FWD:");
        squeeze (c, S[MAX]); //error here
        printf("\n");
        printf("BWD:");
        printBackwards (S[MAX]); //error here
        printf("\n");
        return 0;
}

void squeeze (char c, char S[])
{
        int i;
        char fwd[MAX];
        for ( i = 0; i < MAX; i++)
        {
                if ( S[i] == c )
                {
                        S[i] = ''; //errorr here
                }
        fwd[MAX] += S[i];
        }
        printf ("%c", &S[i]);
}

void printBackwards (char S[])
{
        int j;
        char bckward[MAX];
        for ( j = sizeof(S); j >= S[0]; j--)
        {
            bckward[MAX] += S[j];
        }
        printf("%c", &bckward[MAX]);
}

My errors:
lab09.c: In function 'main':
lab09.c:20: warning: passing argument 2 of 'squeeze' makes pointer from integer without a cast
lab09.c:23: warning: passing argument 1 of 'printBackwards' makes pointer from integer without a cast
lab09.c:36:11: error: empty character constant

Recommended Answers

All 5 Replies

@line 17
why did you use %c as a parameter when you want to take a string shouldn't that be %s

I tried replacing the %c with %s for char S[], but I'm still getting the same errors... I thought that S[] takes %c because technically its an character array that is being used as a string...? I could be wrong, I'm just a beginner with C.

try passing the array like this

  squeeze (c, S); 
  printBackwards (S);

well as for my previous comment
for the char array/string I was checking if you wanted to use a char or a string for it's values
anyway did you remove the & if your going to use a string

An example with no error checking or optimization.

void squeeze (char c, char S[])
{
    int i = 0;
    int j = 0;
    char fwd[MAX];
    memset(fwd, 0, MAX);

    for ( i; i < MAX; i++)
    {
        if ( S[i] != c )
        {
            fwd[j] = S[i];
            j++
        }
    }
    memcpy(S, fwd, MAX);

    printf ("%s", S);
}

Look up fgetc and fgets for your input and call the functions as suggested above.

include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
char fwd[MAX];
void squeeze (char c, char S[]);
void printBackwards (char S[]);
int main (void)
{
    char S[MAX], c;
    printf("Enter a character: ");
    scanf("%c", &c);
    printf ("Enter a string max 99 characters: ");
    scanf("%s", S);

printf("\n");
printf("FWD:");
squeeze (c, S); //error here
printf("\n");
printf("BWD:");
printBackwards (fwd); //error here
printf("\n");
return 0;
}

void squeeze (char c, char S[])
{
int i;
int pos = 0;

for ( i = 0; S[i] != '\0'; i++)
{
    if ( S[i] == c )
    {
        continue;
    }
    fwd[pos++] += S[i];
}
fwd[pos] = '\0';
printf("%s", fwd);
}
void printBackwards (char S[])
{
int j, pos;
char bckward[MAX];

for ( j = strlen(S), pos=0; j >= 0; j--, pos++)
{
    bckward[pos] = S[j];
    printf("%c", S[j]);
}

    bckward[pos] = '\0';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.