Concatenate Strings in C

Asked

Viewed 2,007 times

3

Good afternoon, you guys!

I have a problem in Strings using the C Language.

The problem asks the user to enter a name, then to enter 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 didn’t understand how to add it in the last vowel of the string.

2 answers

3


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

There may be some more efficient way, or even a function that can do this, but the logic must be the same.


Logic

1. You must first locate the last vowel

2. shall pull forward qtd sometimes to be able to put the vowel.

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

3. Putting the letter in the right 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 in the ideone

  • The name may not have any vowel, which can cause problems in this program (because of the 1st for, if you find nothing i will have value of -1 and will add in it)
  • This program assumes it will have lowercase vowels
  • This program does not guarantee that it does not reach the overflow in name as it can pull forward in such a way that it can reach more than 100. Ex: qtd=100
  • 1

    The problem of vowel absence in a user input could not be solved using a drop in 1for ? If you can’t find a vowel direct to the end of the program.

  • 1

    Could create a variable int x=-10 then at the end of the first cycle if(x==-10) did the end drop like I said.

-1

You can do a control to capture uppercase and minuscule 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;

}

Browser other questions tagged

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