Segmentation failure when printing white space C

Asked

Viewed 63 times

-1

I need to do a function that returns a doorman to the first blank space found in the string. When I try to print the blank space the program accuses segmentation failure, which does not happen when I change *str == ' ' by any character, for example (*str == 'b').

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

int main()
{

    char word[80];

    printf("Digite a string: \n");
    scanf("%s", &word);

    char *firstblankchar(char *str)
    {
        while (*str)
        {
            if (*str == ' ')
            {
                return str;
            }

            *str++;
        }
    }

    printf("%c", *firstblankchar(word));
}

2 answers

1

There are several problems with this code.

The scanf() has trouble reading texts with spaces so I switched to something that reads correctly.

It doesn’t make much sense to return the first space, but I left so.

You’re adding the character you’re manipulating into firstblankchar() but what you want to increment is the pointer, so don’t use the operator to dereference, just increment the pointer itself.

What happens if you don’t find a space in the text? You’re returning nothing, and it can’t happen, you have to return something. I have returned 0 which is a null pointer. Doing this the right thing is to test the return to see if it came 0 and not let use the data, only if another address comes it should access the desired character, so I put the return, but I let you do the test.

See how it looks:

#include <stdio.h>

char *firstblankchar(char *str) {
    while (*str) {
        if (*str == ' ') return str;
        str++;
    }
    return 0;
}

int main() {
    char word[80];
    printf("Digite a string:\n");
    fgets(word, 79, stdin);
    printf("%c", *firstblankchar(word));
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Perfect, thank you so much for your help

  • Why the 79 in fgets? I’ve seen several exercises with this 79 but I don’t understand. Why 79 and not 80 or 78?

  • Could be another number and to speak the truth I preferred for 80, but for that I would have to have a buffer of 81, when only 80 has to be 79, because of the terminator: https://answall.com/q/327101/101 and https://pt.overfstacklow.com/q/136628/101

0


I need to do a function that returns a doorman to the first blank space found in the string. When I try to print the blank space program shows segmentation failure, which does not happens when I change *str == ' ' by any character, by example (*str == 'b')

Your question was edited so I don’t know what you wrote and what was changed. Note that it is written as it is in the text, str='' no space between quotes in the program that is different from str=' ' which is what you intended to test.

Understand that in C *str++ sum one in the value that is in *str. If you want to increase the pointer str so that he points to the next letter he must write str++. In general those operators ++ and -- are more problem than solution, even if gentle. str++ and ++str has subtle difference, with any anyone should worry if not use... But so it is.

Back to your program, and a slightly altered example

Take this example here and maybe it will help you understand the mechanics of this in C

The program shows

Frase original: 'uma frase para testar o programa    '
Depois do proximo branco: ' frase para testar o programa    '
Depois do proximo branco: ' para testar o programa    '
Depois do proximo branco: ' testar o programa    '
Depois do proximo branco: ' o programa    '
Depois do proximo branco: ' programa    '
Depois do proximo branco: '    '
Depois do proximo branco: '   '
Depois do proximo branco: '  '
Depois do proximo branco: ' '
Depois do proximo branco: '(null)'
Depois do proximo branco: '(null)' :)

Here is the example

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

char* firstBlankChar(char*);

int main()
{
    char word[80] = "uma frase para testar o programa    ";
    char* proximo = word;
    printf("Frase original: '%s'\n", word);
    while (proximo != NULL)
    {
        proximo = firstBlankChar(1+proximo);
        printf("Depois do proximo branco: '%s'\n", proximo);
    };
    printf("Depois do proximo branco: '(null)' :)\n");
};

char* firstBlankChar(char* str)
{
    while (*str != 0)
    {
        if (*str == ' ') return str;
        str += 1;
    };
    return NULL;
};

In this example the program keeps looking for spaces and shows on the screen the remaining string, until the string is finished.

Note this folkloric behavior of printf(): When the string value is 0, the one NULL and that will exit the loop and finish the program it prints

Depois do proximo branco: '(null)'

and you’ll never know the difference between that and the result of this command here

printf("Depois do proximo branco: '(null)'\n");

Of course not every compiler will do this in every situation.

Note that in general if you are going to print a string that may have spaces or just spaces it is convenient to put something around, like the quotes I used here, for obvious reasons.

Avoid reading things from your keyboard before your program is ready. You will learn nothing from these lines printf() and fgets() and scanf() and things like that. But it will take much longer to test your program...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.