-3

This is the code that I came up with:

#include<stdio.h>
#include<string.h>

void delit(char *s, int i) {
  int n = 0;
  for (int j = i; j < strlen(s) - 1; j++) {
    s[j] = s[j + 1];
  }
  printf("%s", s);
}

int main() {
  char s[100];
  printf("gimme string with less than 100 chars");
  gets(s);
  int l = strlen(s);
  for (int i = 0; i <= l - 2; i++) {
    if (s[i] == s[i + 1] && s[i] == ' ') {
    delit(char *s, int i);
    }
  }
  printf("after processing %s", s);
}

This is not working and showing 2 errors :

//too few arguments to function 'delit'{delit(char *s, int i);}

//ction 'main':tempCodeRunnerFile.c:20:16: error: expected expression before 'char'{delit(char *s, int i);}

Please advise.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    `{delit(char *s, int i);}` this is not how you call a function in C. Just pass the variables, not their types: `{delit(s, i);}` – yano Aug 30 '21 at 16:17
  • You put the types in your function call, you only put those in when defining the function, `delit(s,i)` – Samuel Aug 30 '21 at 16:17
  • 1
    Never ***ever** use `gets`! [It's so dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that is has been removed from the C language. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Aug 30 '21 at 16:18
  • @Samuel Ok I fixed it it's working but only for two spaces. For eg with 3 blanks it returns 2 blanks not one?? – glorytothe12suns Aug 30 '21 at 16:31
  • @yano Ok I fixed it it's working but only for two spaces. For eg with 3 blanks it returns 2 blanks not one?? – glorytothe12suns Aug 30 '21 at 16:31
  • The standard C functions like `printf` and `strlen` are really no different from your own functions. You know how to call them. So you should also know how to call your own function. Was it a copy-paste typo? Or do you really don't know how to call functions? – Some programmer dude Aug 30 '21 at 16:48
  • glorytothe12suns, NMDV, yet "WHy ... downvoting?" Poorly formatted code, slang, using `gets()`, "This is not working" is vague. lack of a clear quesiton, asking for [more debug](https://stackoverflow.com/questions/68987228/i-have-to-replace-multiple-blanks-in-a-string-with-a-single-blank-in-c#comment121924359_68987228) after answring the question all attact DVs. Review [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). On the plus side, good to post the exact error messages. – chux - Reinstate Monica Aug 30 '21 at 16:56

2 Answers2

0

Your main problem is your attempt to call the function delit(char *s, int i). Inside of main() you call the function using delit(char *s, int i). You don't need to put the types in the function call, so it should just be delit(s, i).

That should make your code compile.

To handle the multiple spaces you could change your loop in main to only advance if the characters are NOT both spaces:

for (int i=0;i<=l-1;)
{
    if (s[i]==s[i+1] && s[i]==' ')
    {
        delit(s,i);
    }else{
        i++;
    }            
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sean
  • 49
  • 4
0

This function is incorrect.

void delit(char *s, int i) {
  int n = 0;
  for (int j = i; j < strlen(s) - 1; j++) {
    s[j] = s[j + 1];
  }
  printf("%s", s);
}

Let's assume that the passed string is " A". The length of the string is equal to 3, It is stored as a character array { ' ', ' ', 'A', '\0' }.

So the loop will look like

  for (int j = 0; j < 2; j++) {

and after the loop you will get the string { ' ', 'A', 'A', '\0' } instead of the correct string { ' ', 'A', '\0' }.

Also, it is inefficient to move all characters to the left after it finds a duplicate space character.

Within main this record

delit(char *s, int i);

is an invalid function declaration with an absent return type instead of a function call.

Also the function gets is unsafe and is not supported by the C Standard. Instead, use the function fgets.

I would define the function delit the following way as shown in the demonstrative program below.

#include <stdio.h>

char * delit( char *s, char c )
{
    if ( c != '\0' )
    {
        for ( char *p = s, *q = s; *p++; )
        {
            if ( *p != c || *( p - 1 ) != c )
            {
                if ( ++q != p ) *q = *p;
            }
        }
    }       
    
    return s;
}

int main(void) 
{
    char s[] = "Hello     world!";
    
    printf( "\"%s\"\n", s );
    printf( "\"%s\"\n", delit( s, ' ' ) );

    return 0;
}

The program output is

"Hello     world!"
"Hello world!"
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335