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...
Perfect, thank you so much for your help
– Bianca Caetano
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?
– Natan Fernandes
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
– Maniero