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?
Use
strcat
simply can not meet the problem in question.– Jefferson Quesado
I thought you just wanted to separate the two strings...
– System_4n4n
With this intention of "separating two strings"
strcat
It won’t help either– Jefferson Quesado
#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; }
– System_4n4n
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
– Jefferson Quesado
hummmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm!!!
– System_4n4n