Why don’t my strings concatenate correctly?

Asked

Viewed 50 times

-4

I want to display information in the following format: last surname/first name. But it keeps skipping a line in the concatenation. At least, that’s what I think is happening...

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


/*
Síntese
Objetivo: ler nome completo de uma pessoa e exibir, separado por uma barra, o último sobrenome e o primeiro nome
Entrada: Nome da pessoa
Saída: Ultimo sobrenome/Primeiro nome
*/

#define MAX_NOME 256
#define ERRO printf("\nSo pode conter letras!\n");
void apresentaNome(char * nome);
void leValidaTexto(char * msg, char * msgErro, char nome[]);
int main(int argc, char *argv[]) {
    char nome[MAX_NOME];
    leValidaTexto("Digite o nome completo:", "\nTexto invalido!\n", nome);
    apresentaNome(nome);
    return 0;
}
void leValidaTexto(char * msg, char * msgErro, char nome[]){


    int flag = 1, i=0;
    int tam;

    do{
        printf("%s", msg);
        fgets(nome, MAX_NOME, stdin);
        tam = strlen(nome)-1;

        if(tam==0){
            printf("%s", msgErro);
            flag = 0;
        }else{
            for(i=0; i < tam; i++){
                if(isdigit(nome[i])!=0){
                    ERRO
                    flag = 0;
                }else{
                    flag = 1;
                }
            }
        }

    }while(!flag);


}
void apresentaNome(char * nome){
    int i=0, j=0, k=0, p=0;
    char nomeAux[MAX_NOME], nomeAux2[MAX_NOME];
    char nomeAux3[MAX_NOME];
    char c = ' ', d = ' ';
    for(i=strlen(nome)-1; i>=0; i--){
        if(nome[i] == ' '){
            break;
        }

        c = nome[i];
        nomeAux[j] = c;
        j++;

    }
    //nomeAux[j] = '\0';
    //printf("%s", nomeAux);
    /*
    for(k=strlen(nomeAux)-1;k>=0;k--){
        d = nomeAux[k];
        nomeAux2[p] = d;
        p++;
    }
    */

    strrev(nomeAux);
    for(i=0; i < strlen(nome)-1; i++){
        while(nome[i]!= ' '){
            //printf("%c", nome[i]);
            nomeAux3[i] =nome[i];
            break;
        }
    }


    strcat(nomeAux, "/");
    printf("%s\n", strcat(nomeAux, nomeAux3));

}

1 answer

0


Well, I did not understand very well this problem I totally repeated it taking the ideas, well it does not accept numbers or spaces, to facilitate at the time of showing the names, I stored in an array of characters and showed the first and last name, any questions ask.

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

int verifica(char *frase);
void mostraNaTela(char *frase);
int main(int argc, char** argv)
{
   char nome[20];
   printf("Digite seu nome completo\n");
   scanf(" %[^\n]", nome);
   if(verifica(nome))printf("ERRO\n\n\n");
   else mostraNaTela(nome);

    return 0;
}

int verifica(char *frase)
{
   int i;
   for(i = 0; i < strlen(frase); i++)
   {
       if(!isalnum(frase[i]) && frase[i] != ' ')
       {
          return 1;
       }
       else if(frase[i] >= '0' && frase[i] <= '9')
       {
          return 1;
       }
   }
    return 0;
}


void mostraNaTela(char *frase)
{
   char nomes[30][30];
   char *ptr;
   int cont = 0;
   ptr = strtok(frase, " ");
   while(ptr != NULL)
   {
      strcpy(nomes[cont++], ptr);
      ptr = strtok(NULL, " ");
   }
    printf("%s\\%s\n", nomes[0], nomes[cont - 1]);
}
  • 1

    That’s basically it, Rafael. The only thing that came out different was the way out. First name/last name; the problem asked last name/first name. Got it? I tried to do without using pointer and other functions just to train my logic, which is not the best.

  • 1

    I just don’t understand why this question received so many negative reviews. Anyway, thanks a lot, man!

  • For nothing, regarding the negativity of the question was not very well explained, give next time explain better.

Browser other questions tagged

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