Text line encryptor

  • File name: Text line encryptor
  • Size: 1.84 KB
yes /


The code reads an integer numeric value n, between 1 and 9, to encrypt a line of text that ends with a point. If a character is a letter or digit, is replaced by the character that is n positions later in the set of characters (uppercase, lowercase, digits), we assume that behind the Z is A, and z is 0. Any character other than letter or digit is replaced by a dash '-'

In addition the program will remove all consecutive repetitions of the same character. The program also checks that the entered value for n is within the range order, otherwise it will request another number until the value entered is correct.

If the text you enter is:
El robot R2D2 es el mas chuuuuloooo de la pelicula.
And n = 1
It is printed:


#include <stdio.h>

void main (){

    char car, ant;
    int num, i;

    printf("Introduce un numero entre 1 y 9: ");
    scanf("%d", &num);

    while (num > 9 || num < 1)//Se controla que la entrada de datos es correcta
        printf("Valor incorrecto.\n");
        printf("\nIntroduce un numero entre 1 y 9: ");
        scanf("%d", &num);

    fflush(stdin);   //vacia el buffer, necesario puesto que despues se lee un caracter
    printf ("\nEscribe un texto que acabe con punto: ");
    scanf ("%c", &car);
    ant = car+1;

    while (car != '.')//Si hay un punto, se detiene
        if (car != ant)
            if (car>='a'&& car<='z')
                    printf("%c", car+num +'a'-'z'-1);//Cuando la suma salga de 'z' se le resta para "dar la vuelta"
                else printf("%c",car+num);
            else if (car>='A'&&car<='Z')
                    printf("%c", car+num +'A'-'Z'-1);
                else printf("%c",car+num);
            else if (car>='0'&& car<='9')
                    printf("%c", car+num +'0'-'9'-1);
                else printf("%c",car+num);
            else printf("-");
            ant = car;
            scanf("%c", &car);

No ratings yet - be the first to rate this.

Add a comment

You're using an AdBlock like software. Disable it to allow submit.