I have noticed that when I don't initialize the array in this code that turns lowercase to uppercase the code only does half of the work (it turns Antoine Copepe -> aNTOiNE_cOPEPE
). I was wondering if there were the null reference errors Kevin was talking about in his answer to how much important it is to initialize arrays
#include <iostream>
#include<string.h>
#include<ctype.h>
#include<conio.h>
using namespace std;
void ConvMl_(char [], int);
int main(){
char Nombre[32];
int Len;
cout<< "inserte su nombre completo: ";
cin.getline(Nombre,32);
Len = strlen(Nombre);
ConvMl_(Nombre, Len);
cout<<Nombre<<endl;
getch();
return 0;
}
void ConvMl_(char Nombre[], int Len){
int Arr[Len] = { 'EsNecesarioInicializarElArray' };
for(int i=0;i<Len;i++){
if(Nombre[i] == ' '){
Nombre[i] = '_';
}
}
for(int i=0;i<Len;i++){
if(Nombre[i] >= 'A' && Nombre[i] <= 'Z'){
Nombre[i] = tolower(Nombre[i]);
Arr[i]=1;
}
}
for(int i=0;i<Len;i++){
if(Nombre[i] >= 'a' && Nombre[i] <= 'z' && Arr[i] != 1){
Nombre[i] = toupper(Nombre[i]);
}
}
}
Indeed this gives me:
main.cpp:28:22: warning: character constant too long for its type
inserte su nombre completo: Antoine Coppepe
aNTOINE_cOPPEPE
Whereas when I do int Arr[Len];
I only have Antoine Copepe -> aNTOiNE_cOPEPE