0

The problem is the next, generate a new string, 1. In the firt place : the firts letter of the name 2. In the second place : the third letter of the name 3. In the thitd place : The last letter of the name 4. In the fourth place : the lenght of the character int main(){

char cad1[10]={};
char cad2[4]={};
int  n ; 

cout<<"Ingresa un nombre de 4 o mas letras : " ;
cin.getline(cad1,10,'\n') ;

n =  strlen(cad1) ;
    
cad2[0] = cad1[0] ;
cad2[1] = cad1[2] ;
cad2[2] = cad1[n-1] ;
cad2[3] ??

cout<<cad2;
cout<<endl ;
system("PAUSE") ; return 0 ; 

}

1 Answers1

0

Welcome to Stack Overflow! If I understood your question correctly, you need to save the typed name length on the last char. If that's the case, just do:

cad2[3] = n + '0';
Jonas Castanheira
  • 198
  • 1
  • 1
  • 9
  • Yes you did, thanks can you explain why ? – Henry Villa Apr 20 '22 at 03:09
  • Chars in the C programming language are represented by ASCII values, each value corresponding to a certain char. The ASCII value for an integer of value X is 'X' (i.e. the char '9' is represented by the ASCII value 9). If you add an int and a char, you end up converting the int into a char, but since you're adding 0 to its current ASCII value, you end up with a char that corresponds to the int. Note: THIS ONLY WORKS FOR SINGLE DIGIT INTS! – Jonas Castanheira Apr 20 '22 at 13:37
  • another time thanks so much, i have another cuestion how it works for alot of digits ? – Henry Villa May 01 '22 at 12:36