-3

my teacher told us to write a program that should give the length of a string after taking out the consecutive spaces, and only use one space. for example, if i write "hello(space)(space)(space)world" it should return "hello world" and the length should be 11. The problem is that i keep getting segmentation fault and i don't know why? can someone explain it to me? thanks by the way!

int limpaEspacos (char t[]){

int i, j, w=0;
char c;
for(i=0;t[i];i++){

    t[w]=t[i];
    w++;

    for(j=i;t[j];j++,i++){

        if (t[j]!=' ') {break;}
       }
    }

  t[w]='\0';

  return w;
}


int main () {
int a;

a= limpaEspacos ("b   ruh");

printf("%d\n", a );

return 0;

}
  • Are you facing a particular issue? Please specify so we can know what to look for. – Fabulous Apr 06 '18 at 12:51
  • @Carlos Afonso You may not change string literals. Any attempt to change a string literal results in undefined behavior. – Vlad from Moscow Apr 06 '18 at 12:51
  • `for(j=i;t[j];j++,i++)` Are you not incrementing `i` too much ? – vincrichaud Apr 06 '18 at 12:52
  • @VladfromMoscow i don't know what that means, im still a noob ahaha, can you explain it? – Carlos Afonso Apr 06 '18 at 13:00
  • @Carlos Afonso The argument of this call a= limpaEspacos ("b ruh"); is the string literal "b ruh". Declare a character array initializing it with the string literal and use it in the call as for example char s[] = "b ruh"; a= limpaEspacos ( s ); – Vlad from Moscow Apr 06 '18 at 13:04

1 Answers1

0

"b ruh" is a constant string literal you cannot modify it. That is why you are getting segmentation fault.

Below is the corrected ode, it works now:

#include <stdio.h>
#include <stdlib.h>

int
limpaEspacos (char t[])
{

  int i, j, w = 0;
  char c;
  for (i = 0; t[i]; i++)
    {

      t[w] = t[i];
      w++;

      for (j = i; t[j]; j++, i++)
    {

      if (t[j] != ' ')
        {
          break;
        }
    }
    }

  t[w] = '\0';

  return w;
}


int
main ()
{
  int a;
  char str[100] = "b   ruh";    // code change
  a = limpaEspacos (str);

  printf ("%d\n", a);

  return 0;

}
Syed Waris
  • 1,056
  • 1
  • 11
  • 16
  • 1
    +1, but strictly speaking in C it's a `char[]` type, but it's still UB to attempt to modify it. Cf in C++ where it's a `const char[]` type. – Bathsheba Apr 06 '18 at 13:01
  • 1
    But you've ruined the answer with the "corrected code" (which is a mess). Vote retracted. – Bathsheba Apr 06 '18 at 13:02
  • The person asking the question , wanted end result of length of the input string (`b rah`) after stripping off the spaces. The output is now coming 4 (as expected). – Syed Waris Apr 06 '18 at 13:07
  • For a good answer on Stack Overflow, *everything* you type has to be of the highest quality. Once you start offering a solution, that has to be of the highest quality too. The code you present is faulty, even if you look beyond the formatting. E.g. is `int` a good type for an array index? If I were you, I'd retract the code, merely presenting the fix `char str[100] = "b ruh";`. – Bathsheba Apr 06 '18 at 13:10
  • OK. I understand – Syed Waris Apr 06 '18 at 13:14