-
-
Save alexandregama/391a98872abd2768f8933b870820eb7a to your computer and use it in GitHub Desktop.
C Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdbool.h> | |
#include <stdio.h> | |
#include <string.h> | |
void imprimeNomeInvertido(char nome[]); | |
_Bool textoVazio(char nome[]); | |
void imprimeParOuImpar(int *numero); | |
int main() { | |
char nome[50]; | |
int ru = 0; | |
int *ptr; | |
do { | |
printf("Preencha o seu nome completo: "); | |
fgets(nome, sizeof(nome), stdin); | |
if (textoVazio(nome)) { | |
printf("- é necessário fornecer algum texto\n"); | |
} | |
} while (textoVazio(nome)); | |
printf("Preencha o RU: "); | |
scanf("%d", &ru); | |
*ptr = ru; | |
imprimeParOuImpar(ptr); | |
imprimeNomeInvertido(nome); | |
return 0; | |
} | |
/* | |
Função que recebe uma string e a imprime de forma invertida | |
Foi usado o método strlen() da biblioteca string.h para | |
identificar o tamanho da string | |
*/ | |
void imprimeNomeInvertido(char nome[]) { | |
printf("\nNome invertido: "); | |
int size = strlen(nome); | |
// percorre a string a partir da última posição | |
for (int i = size - 1; i >= 0; i--) { | |
printf("%c", nome[i]); | |
} | |
} | |
/* | |
Função que recebe uma string e valida se existe ou não um texto | |
Foi usado o tipo _Bool da biblioteca stdbool para trabalhar | |
com valores booleanos | |
*/ | |
_Bool textoVazio(char nome[]) { return strlen(nome) == 1; } | |
/* | |
Função para imprimir se um dado número é par ou ímpar | |
*/ | |
void imprimeParOuImpar(int *numero) { | |
// valida se o resto da divisão é zero, indicando um número par | |
if (*numero % 2 == 0) { | |
printf("\nRU é um número par"); | |
} else { | |
printf("\nRU é um número impar"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment