Advertisement
brunohsn6

Atividade 21/08

Aug 21st, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. char* leitura(char nome[]);
  6. char* formatar(char nome[]);
  7. int validacao(char nome[]);
  8.  
  9. int main(){
  10.     char nome[101], nomeFormatado[101];
  11.     int i, flag = 0;
  12.     strcpy(nome, leitura(nome));
  13.     flag = validacao(nome);
  14.     if (flag == 0){
  15.         strcpy(nomeFormatado, formatar(nome));
  16.         printf("%s \n", nomeFormatado);
  17.     }
  18.     else{
  19.         printf("Nome invalido \n");
  20.     }
  21.     return 0;
  22. }
  23.  
  24. char *leitura(char nome[]){
  25.     int i;
  26.     printf("Digite o seu nome: ");
  27.     for (i = 0; i < 100; i++){
  28.         nome[i] = getchar();
  29.         if (nome[i] == '\n'){
  30.             nome[i] = '\0';
  31.             break;
  32.         }
  33.     }
  34.     return nome;
  35. }
  36.  
  37. int validacao(char nome[]){
  38.     int i = -1;
  39.     while (nome[i + 1] != '\0'){
  40.         i++;
  41.         if (isalpha(nome[i]) == 0 && nome[i] != ' '){
  42.             return 1;
  43.         }
  44.     }
  45.     return 0;
  46. }
  47.  
  48. char* formatar(char nome[]){
  49.     char aux[101];
  50.     int i = -1, j = -1, d;
  51.     do{
  52.         i++;
  53.         if (i == 0 && isalpha(nome[i]) != 0) {
  54.             j++;
  55.             aux[j] = toupper(nome[i]);
  56.             i++;
  57.         }
  58.         if (nome[i] == ' '){
  59.             if (isalpha(nome[i + 1]) != 0){
  60.                 if (j != -1){
  61.                     j++;
  62.                     aux[j] = toupper(nome[i]);
  63.                 }
  64.                 j++;
  65.                 aux[j] = toupper(nome[i + 1]);
  66.                 i++;
  67.             }
  68.         }
  69.         else if (isalpha(nome[i]) != 0){
  70.             j++;
  71.             aux[j] = tolower(nome[i]);
  72.         }
  73.     } while (nome[i + 1] != '\0');
  74.     aux[j + 1] = '\0';
  75.     for (d = 0; d < j; d++){
  76.         if (d == 0){
  77.             aux[d] = toupper(aux[d]);
  78.         }
  79.         if (aux[d] == ' '){
  80.             aux[d + 1] = toupper(aux[d + 1]);
  81.         }
  82.     }
  83.     return aux;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement