Ver Mensaje Individual
  #5 (permalink)  
Antiguo 14/11/2014, 01:36
eferion
 
Fecha de Ingreso: octubre-2014
Ubicación: Madrid
Mensajes: 1.212
Antigüedad: 9 años, 7 meses
Puntos: 204
Respuesta: Capitalizar un nombre con c/c++

Cita:
Iniciado por kutcher Ver Mensaje
Para que utilizar un segundo for si con solo uno te basta y sobra un ejemplo:
Te propongo las siguientes mejoras:
* No uses la librería "ctype", es un requisito.
* si str[30], entonces scanf("%30[^\n]", str) es inseguro, te deja introducir 30 caracteres más el correspondiente nulo.
* es más estándar poner return EXIT_SUCCESS a return 0.

Código C++:
Ver original
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void)
  6. {
  7.   char str[31];
  8.   bool toCapital = true;
  9.   char* ptr = str - 1;
  10.  
  11.   printf("Introduce tu nombre completo: ");
  12.   scanf("%30[^\n]", str);
  13.  
  14.   while( *++ptr )
  15.   {
  16.     if( *ptr == ' ' )
  17.       toCapital = true;
  18.     else if( toCapital == true )
  19.     {
  20.       if ( *ptr >= 'a' && *ptr <= 'z' )
  21.         *ptr += ('A' - 'a');
  22.       toCapital = false;
  23.     }
  24.     else
  25.     {
  26.       if ( *ptr >= 'A' && *ptr <= 'Z' )
  27.         *ptr += ('a' - 'A');
  28.     }
  29.   }
  30.  
  31.   printf("%s\n", str);
  32.  
  33.   return EXIT_SUCCESS;
  34. }