Why am i receiving a bus error?
I´m making a simple program that abbreviate the middle names, for example, we have Artur José Bastos Costa and it should print “Artur J. B. Costa”.
Here is my code: enter image description here enter image description here
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* abrevia(char full_name[]){
int numNames = 0, i = 0;
char* abbreviation = (char*) malloc(strlen(full_name) * sizeof(char));
char* name = strtok(full_name, " ");
strcpy(abbreviation, name);
strncat(abbreviation, " ", 1);
while (name != NULL){
numNames++;
name = strtok(NULL, " ");
}
char* nomeCopy = (char*) malloc(strlen(full_name) * sizeof(char));
strcpy(nomeCopy, full_name);
char* name2 = strtok(nomeCopy, " ");
while (name2 != NULL){
if (i != numNames -1){
strncat(abbreviation, &name2[0], 1);
abbreviation[strlen(abbreviation)] = '.';
abbreviation[strlen(abbreviation)] = ' ';
}
else {
strcat(abbreviation, name2);
}
name2 = strtok(NULL, " ");
i++;
}
free(nomeCopy);
return abbreviation;
}
int main(){
char* abr = abrevia("Artur José Bastos Costa");
printf("%s", abr);
free(abr);
return 0;
}
First i didn´t copied the full_name, but i tried doing that becausa i was using strtok twice and there wasn´t more tokens in the first time, so i tought that in the second loop it will crash because of that. But i couldnt fix it. Sorry my english is not my first language
abrevia()butstrtok()modifies it's first argument. You can pass an array like this:or even better if you want to use
strtok()(opposed tostrpbrk()) haveabrevia()copy the string into an array. This allows you to make the input a pointer to a constant value.malloc(strlen(full_name))is 1 byte too small.Prefer passing a variable instead of a type of
sizeof(). Less code duplication. Assizeof(char)is defined as 1 so I would just leave it out.Don't cast the
void *frommalloc().You don't do anything with the 2nd middle name.
The basic idea is to handle the first and last token as special cases. To figure out if it's the last token you need to do 1 token look-ahead:
and here is the output: