Concatenate Strings in C

Good afternoon, guys!

I'm having a problem with Strings using the C Language.

The problem asks the user to type a name, then type A special character and finally how many times he wants the character to be concatenated with the String. However, concatenation should only be performed with the last vowel of the String.

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

int main(){

char nome[100]="Celular";
char letra[100];
int qtd=0;

printf("Nome definido: %s\n\n", nome);

printf("\nCARACTER que deseja INCLUIR no nome ");
scanf(" %c",&letra);

printf("\nQuantidade de vezes que deseja colocar no nome? ");
scanf(" %d",&qtd);

for(int i=0;i<qtd;i++){

    strncat((nome), letra, 1 );
}

printf("resultado = %s\n ", nome);

system("PAUSE");
return 0;

}

The code will add the special character at the end of the string. However, I did not understand how to be added in the last vowel of the string.

Author: Leonardo Basilio, 2018-09-08

2 answers

I made the basic idea of the program and I will explain the logic, but this program fails in some cases that I will also say, will have after having some validations for this code to work for the various names, it is useful to think about the subject and not do soon a copy paste in the code.

There may be some more efficient method, or even a function that can do this, but the logic will have to be the same.


Logic

1. You must first locate the ultima vowel

2. you must pull forward qtd several times to place the vowel.

Ex: qtd=2-- > cell_ _r, then in that space we put the letter we want

3. Put the letter in the correct place


char nome[100]="Celular";
char letra;
int qtd=0;
int i, j;
printf("Nome definido: %s\n\n", nome);

printf("\nCARACTER que deseja INCLUIR no nome ");
scanf(" %c",&letra);

printf("\nQuantidade de vezes que deseja colocar no nome? ");
scanf(" %d",&qtd);

for( i=strlen(nome) ; i>-1; i--){ /** 1 **/

    if(nome[i]=='a' || nome[i]=='e' || nome[i]=='i' || nome[i]=='o' || nome[i]=='u' )
    {
        i++;
        break;
    }

}

for(j=strlen(nome); j>=i; j--) /** 2 **/
    nome[j+qtd]=nome[j];

for(j=i; j<qtd+i; j++) /** 3 **/
    nome[j]=letra;

printf("resultado = %s\n ", nome);

Code no ideone

  • the name may not have any vowels, which can cause problems in this program (because of the 1st for, if it does not find anything i will have value of -1 and will add in it)
  • this program assumes that it will have lowercase vowels
  • this program does not guarantee that it will not reach the overflow in the name, since it can pull forward in such a way that it can reach more than 100. Ex: qtd=100
 3
Author: Fábio Morais, 2018-09-08 20:55:24

You can make a control to capture uppercase and lowercase vowels, as well as avoid unforeseen events if a typed word does not have a vowel as follows:

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

int main(){

 char nome[100];
 char temp[100];
 char letra[2];
 int qtd,i,j,k,encontrouVogal = 0;


 printf("Digite um nome:", nome);
 fgets(nome,100,stdin);
 nome[strcspn(nome,"\n")] = 0;

printf("\nCARACTER que deseja INCLUIR no nome: ");
scanf(" %c",letra);

printf("\nQuantidade de vezes que deseja colocar no nome?: ");
scanf(" %d",&qtd);

for(i= strlen(nome); i > -1 ;i--){
   if(nome[i] == 'a' || nome[i] == 'e' || nome[i] == 'i' || nome[i] ==
   'o' || nome[i] == 'u'){
    encontrouVogal = 1;
    break;
  }else if(nome[i] == 'A' || nome[i] == 'E' || nome[i] == 'I' || nome[i] == 'O' || nome[i] == 'U'){
    encontrouVogal = 1;
    break;
  }
}

if(encontrouVogal){

     /* Todos os caracteres encontrados apos a vogal são passados
     para uma string temporaria */
     for(j = i+ 1,k= 0; j < strlen(nome); j++,k++){
       temp[k] = nome[j];
     }
     temp[k] = '\0';

     /* Todos os caraceteres encontrados apos a vogal são removidos */
     for(j = i + 1 ; j < strlen(nome); j++){
       nome[j] = '\0';
     }

     /* A concatenação é feita com o caractere escolhido */
     for(j = 0; j < qtd ; j++){
       strcat(nome,letra);
     }

     /* Todos os caracteres que estão na string temporaria são passados
     novamente para a string nome*/
     strcat(nome,temp);

     printf("resultado = %s\n ", nome);

   }else{
   printf("A palavra digitada nao possui vogais,impossivel concatenar!\n");
  }

 system("PAUSE");
 return 0;

}
 -1
Author: Igor PTZ, 2018-09-08 23:38:51