Two strings starting from one, in C

Asked

Viewed 354 times

0

Hello, I need a software that reads a full name, composed of a simple name and a surname, separated by "_" and prints them separately.

The code should use the following main function:

#include <stdio.h>

int main(){
    char nome[30];
    char * sobrenome;
    scanf("%s", nome);
    sobrenome = extraiSobrenome(nome);
    printf("Nome\n%s\nSobrenome\n%s\n",nome, sobrenome );
    return 0;
}

I tried to write the function extraiSobrenome(nome);, but without success:

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

extraiSobrenome(nome){
    int i=0, j=0, aux;
    char nova[30], michael[30];
    char sobrenome[30];
    strcpy(nova, nome);
    for(i=0;i<(int)strlen(nova);i++){
        if(nova[i]==95){
            i++;
            break;
        }
    }
    aux=i;
    while(i<strlen(nova)){
        sobrenome[j]=nova[i];
        i++;
        j++;
    }
    sobrenome[strlen(nova)-aux] = '\0';

    return sobrenome;
}

int main(){
    char nome[30];
    char * sobrenome;
    scanf("%s", nome);
    sobrenome = extraiSobrenome(nome);
    printf("Nome\n%s\nSobrenome\n%s\n", nome, sobrenome );
    return 0;
}

The problem is not only to be able to separate the surname, but to change the name too, can help me?

2 answers

2


Failed to declare function return extraiSobrenome. That in the case would be a character pointer, or char*.

Note that you need to change the variable nome original, otherwise you will not succeed in printing nome separately.

You could use the standard library function strtok to make that division every _, but I believe that here the interest is not in using the standard library but in training writing algorithms in C.

An appropriate version of extraiSobrenome without using the standard library:

char *extraiSobrenome(char *nome_com_sobrenome) {
  char *fim_nome = nome_com_sobrenome;
  while (*fim_nome != '_' && *fim_nome != '\0') {
    fim_nome++;
  }
  if (*fim_nome == '\0') {
    // oops, fim da palavra antes de achar o delimitador '_', devo retornar nulo ou um sobrenome vazio?
    return fim_nome; // estou retornando um sobrenome vazio para evitar falhas de segmentação
  }
  // se não caiu no 'if' anterior, então garanto que achou um '_'
  * fim_nome = '\0'; // estou marcando o fim do nome, substituindo o '_' pelo caracter nulo, portanto indicando que a string acabou
  return fim_nome +1; // usando aritmética de ponteiros para indicar que o sobrenome começa na próxima letra
}

Some other points I would like to raise about your code:

  • you compared a character to 95; ok, in the end letters are numbers with zeros and ones, but what is 95?
    Why not compare directly with '_'? Much more expressive this comparison and everyone who read your code and who does not know the decorated ASCII table will understand this version of the comparison
  • you returned an internally created vector in the function, and that’s not cool...
    These variables are stored in the stack, and they only make sense within the context of the execution of the function itself; when it returns, there is no more way to guarantee the meaning of the rest of the stack of the last call... and this code is valid syntactically because internally C works only with the vector address, then you are returning an address of a value that is in the stack that can be overwritten at any time.

Read more about stack, heap, variable scope... I will list some reading suggestions:

About strtok:

-2

What do you think of after recording the first name you add a space like this:

strcat(nome, " ");
  • Use strcat simply can not meet the problem in question.

  • I thought you just wanted to separate the two strings...

  • With this intention of "separating two strings" strcat It won’t help either

  • #include <stdio. h> #include <string. h> int main() ' char first name[10]; char last name[10]; printf("Enter first name: "); gets(first name); strcat(first name, "); printf(" nInform surname: "); (surname); strcat(first name, surname); printf("%s", name) Return 0; }

  • Have you read that the name and surname are given divided by a '_'? So, no, there will be given two readings... Note also that the printing is with separate name and surname, the intention is not to join

  • hummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm!!!

Show 1 more comment

Browser other questions tagged

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