Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- char* leitura(char nome[]);
- char* formatar(char nome[]);
- int validacao(char nome[]);
- int main(){
- char nome[101], nomeFormatado[101];
- int i, flag = 0;
- strcpy(nome, leitura(nome));
- flag = validacao(nome);
- if (flag == 0){
- strcpy(nomeFormatado, formatar(nome));
- printf("%s \n", nomeFormatado);
- }
- else{
- printf("Nome invalido \n");
- }
- return 0;
- }
- char *leitura(char nome[]){
- int i;
- printf("Digite o seu nome: ");
- for (i = 0; i < 100; i++){
- nome[i] = getchar();
- if (nome[i] == '\n'){
- nome[i] = '\0';
- break;
- }
- }
- return nome;
- }
- int validacao(char nome[]){
- int i = -1;
- while (nome[i + 1] != '\0'){
- i++;
- if (isalpha(nome[i]) == 0 && nome[i] != ' '){
- return 1;
- }
- }
- return 0;
- }
- char* formatar(char nome[]){
- char aux[101];
- int i = -1, j = -1, d;
- do{
- i++;
- if (i == 0 && isalpha(nome[i]) != 0) {
- j++;
- aux[j] = toupper(nome[i]);
- i++;
- }
- if (nome[i] == ' '){
- if (isalpha(nome[i + 1]) != 0){
- if (j != -1){
- j++;
- aux[j] = toupper(nome[i]);
- }
- j++;
- aux[j] = toupper(nome[i + 1]);
- i++;
- }
- }
- else if (isalpha(nome[i]) != 0){
- j++;
- aux[j] = tolower(nome[i]);
- }
- } while (nome[i + 1] != '\0');
- aux[j + 1] = '\0';
- for (d = 0; d < j; d++){
- if (d == 0){
- aux[d] = toupper(aux[d]);
- }
- if (aux[d] == ' '){
- aux[d + 1] = toupper(aux[d + 1]);
- }
- }
- return aux;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement