简体   繁体   English

如何在字符串C中重复字符

[英]How to repeat characters in a string C

https://stepik.org/lesson/192229/step/5?unit=166729 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. 任务:使用另一个字符串加密一个字符串,程序需要2个字符串,第一个用作密钥,另一个将被加密,该密钥字符串按字母顺序使用,例如: A = 0,B =1。第二个字符串中的字符将按字母顺序更改。字母可以是小写或大写字母,因为输出将以大写字母表示。

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 ) 密钥不能超过15个字符,第二个字符串不能超过30个字符。字符串只能包含字符。否则,程序将给出falsche eingabe(德语翻译=输入错误)

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 我正在尝试找到一种方法来做。虽然我已经做了很多,但是仍然有问题。我已经给Capital写了小写字母,并按字母A = 0 B的顺序写了键字符= 1 c = 2并且我已经确定当字母结束时会转到字母的开头,例如: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 !! 当密钥短于加密字符串时,问题就开始了。当输入==> cc bbbb时应该是==> DDDD但给了DDBB我需要重复密钥中的字符以使其正确。有人帮助我,将非常感谢!

#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 %

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. 它始终在0到要除以1的数字之间。因此,该公式将遍历较小的数组。

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. 在此示例中, encode_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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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