简体   繁体   中英

How to repeat characters in a string C

https://stepik.org/lesson/192229/step/5?unit=166729

Task : Encrypting a string by using another string.The program wants 2 character strings.The first one is used as a key and the other one is the one will be encrypted.The key string is used with the order of alphabet, for example: A = 0 , B= 1.The characters in second string will be changed in the order of alphabet.The letters can be small or capital it doesn't matter because the output will be in Capital letters.

The key can not be more than 15 characters and second string can not be longer than 30 characters.Strings can only contain characters.If not the program will give falsche eingabe ( german translation = wrong input )

Sample Input 1:
aaaaaaaaaaaaaaaa bob

Sample Output 1:
falsche eingabe

Sample Input 2:
aaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

Sample Output 2:
falsche eingabe

Sample Input 3:
zzzz zzzzzz

Sample Output 3:
YYYYYY

Sample Input 4:
hallo welT

Sample Output 4:
DEWE

Sample Input 5:
AbC HaLLo

Sample Output 5:
HBNLP

Hi ! I am trying to find a way to do it.I've managed to do lots of it but i still have a problem.I've already made small letters to Capital and written keys characters as in the order of alphabet A=0 B=1 c=2 and i've made that when the alphabet ends it goes to the beginning of alphabet for example : bb zz => AA

THE PROBLEM starts when key is shorter than the encrypted string.When the input is ==> cc bbbb it should be ==> DDDD but it gaves DDBB.I need to repeat the characters in the key in order to make it right.If someone help me it would be great thanks !!

#include <stdio.h>


int main() {
char a[30]; // KEY
char b[50]; // encrypted
int i,j,g; // length of char a,b and for loop
scanf("%s %s",&a,b);

for (i = 0; a[i] != '\0'; ++i);
for (j = 0; b[j] != '\0'; ++j); // length found!

if(i>15 || j>30){ //checking if lengths are longer than 15 and 30
    printf("falsche eingabe");
    return 0;
}
    for(g=0;g<i;g++){
    if ((a[g] < 'A' || a[g] > 'Z') && (a[g] < 'a' || a[g] > 'z') || (b[g] < 'A' || b[g] > 'Z') && (b[g] < 'a' || b[g] > 'z')) {
        printf("falsche eingabe"); // checking if inputs are characters
        return 0;
    }
}       // the end Falsche Eingaben
for (g = 0; g < i; g++) {
    if (a[g] >= 'a' && a[g] <= 'z') {
        a[g] = a[g] - 32; // small letters become Capital 
    } 
}   
    for (g = 0; g < j; g++) {
    if (b[g] >= 'a' && b[g] <= 'z') {
        b[g] = b[g] - 32; // small letters become Capital
    } 
}
for (g = 0; g < i; g++){
    a[g]=a[g]- 65 ;      // the number for alphabet z.B A=0 B=1
    if(b[g]+a[g] > 'Z'){
            b[g] = b[g] - 26;   
    } b[g]=a[g]+b[g];
}


printf("%s",b);


return 0;

}

There is a simple way to loop over a short array while at the same time running over a longer one: just use modulo % .

Modulo is the remainder when you divide two numbers. It will always be between 0, and the number you are dividing by minus 1. So this formula will loop over the smaller array.

Use the index in to the longer array modulo the length of the shorter array as the index in to the shorter array.

Here is an example:

char *key = "ABC";
char *string = "Thisisalongstringtoencode";
char encoded[100];
int i;
int key_lenght = strlen(key);

for (i = 0; i < strlen(string); i++) {
    ecoded[i] = encode_character(string[i], key[i % key_length]);
}

In this example, encode_character is a function representing whatever calculation you need to do to encode one character.
You don't have to write such a function, just use its parameters in your calculation.

I've done it after many hours.The only thing it needed was an easy percentage sign (%).

#include <stdio.h>


int main() {
char a[30]; // schluessel
char b[50]; // unverschlüsselter
char c[50]; // verschlüsselter
int i,j,g,e; // für definieren die lange der char a,b
scanf("%s %s",&a,b);

for (i = 0; a[i] != '\0'; ++i);
for (j = 0; b[j] != '\0'; ++j); // Lange gefunden!

if(i>15 || j>30){
    printf("falsche eingabe");
    return 0;
 }
    for(g=0;g<i;g++){
    if ((a[g] < 'A' || a[g] > 'Z') && (a[g] < 'a' || a[g] > 'z')) {
        printf("falsche eingabe");
        return 0;
    }
}       // die Ende für Falsche Eingaben

        for(g=0;g<j;g++){
    if ((b[g] < 'A' || b[g] > 'Z') && (b[g] < 'a' || b[g] > 'z')) {
        printf("falsche eingabe");
        return 0;
    }
}

 for (g = 0; g < i; g++) {
    if (a[g] >= 'a' && a[g] <= 'z') {
        a[g] = a[g] - 32; // kleine zu große

    } 
 }  
    for (g = 0; g < j; g++) {
    if (b[g] >= 'a' && b[g] <= 'z') {
        b[g] = b[g] - 32; // kleine zu große

    } 
  }

  for (g = 0; g < i; g++){
    a[g]=a[g]- 65 ;      // die Zahl für Alphabet z.B A=0 B=1

    }
  //    printf("%d %d\n",i,j);

   for(g=0;g<j;g++){
            if(j>i){ // wenn die länge der b array ist größer als a array,wir 
wiederholen die characters in array
    b[g]=b[g]+a[g%i];
    } else if (i>j){ // wenn die länge der a array ist größer als b array,nur 
addieren
    b[g]=a[g]+b[g];
    } else {
    b[g]=a[g]+b[g]; 
    }
    if(b[g]>90){ // wenn die alphabet endet, gehen wir züruck zu anfang
            b[g] = b[g] - 26;   
    }
  }


 printf("%s",b);


return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM