Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 加密字符串_C_Arrays_String_For Loop - Fatal编程技术网

C 加密字符串

C 加密字符串,c,arrays,string,for-loop,C,Arrays,String,For Loop,我正在编写一个程序,以这种方式对给定字符串进行加密: 如果我们有一个整数V和一个只包含元音的数组V={a,e,i,o,u},如果字符串的字母是元音,那么用它前面的元音替换它,只考虑元音数组(而不是整个字母表!) 要明确的是: String: "uuuu" and V:2 -----> String_out: "iiii" String: "aaaa" and V:2 -----> String_out: "oooo" String: "iiii" and V:2 ----->

我正在编写一个程序,以这种方式对给定字符串进行加密:

如果我们有一个整数V和一个只包含元音的数组V={a,e,i,o,u},如果字符串的字母是元音,那么用它前面的元音替换它,只考虑元音数组(而不是整个字母表!)

要明确的是:

String: "uuuu" and V:2 -----> String_out: "iiii"
String: "aaaa" and V:2 -----> String_out: "oooo"
String: "iiii" and V:2 -----> String_out: "aaaa"
为了解决我的问题,我写了:

    V=2;
    char vow[5]={'a','e','i','o','u'};
    for(i=0;i<strlen(s);i++){
        flag=0;
    for(j=0;j<5 && flag==0;j++){
        if(*(s+i)==vow[j]){
            flag=1;
    }
        if(flag)
           *(s+i)=vow[j-V%5];
    }
那么,我如何解决字母a和e的问题,使我不再在誓言中获得负数[…],并正确遵守命名规则?
如果有不清楚的地方,请告诉我,并提前谢谢

我假设对于a和e,你应该绕着a->o和e->u

改变这个

j-V%5

示例

V=2, j=1

j-V%5 ---------------> -1
(j-V%5) + 5 --------->  4
((j-V%5) + 5) % 5 --->  4

Result:
'e' --> 'u'

我假设对于a和e,你应该绕着a->o和e->u

改变这个

j-V%5

示例

V=2, j=1

j-V%5 ---------------> -1
(j-V%5) + 5 --------->  4
((j-V%5) + 5) % 5 --->  4

Result:
'e' --> 'u'
如果字符串只有元音i、o、u,则此代码有效,但如果字符串有a、e,则输出将错误:

  • 这是因为当元音是
    a
    e
    时,
    j-V%5
    是负数(因为,j是
    0
    1
    ),正如您正确地提到的

  • 只需使用
    (5+j-(V%5))%5
    即可避免
    vow[]
    数组的负索引值

    //when j=0 i.e, vowel `a`
    (5+j- (V%5))%5 = (5+0-(2%5))%5 = 3
    
    //when j=1 i.e, vowel `e`
    (5+j- (V%5))%5 = (5+1-(2%5))%5 = 4
    
    //when j=2 i.e, vowel `i`
    (5+j- (V%5))%5 = (5+2-(2%5))%5 = 0    
    
    //when j=3 i.e, vowel `o`
    (5+j- (V%5))%5 = (5+3-(2%5))%5 = 1
    
    //when j=4 i.e, vowel `u`
    (5+j- (V%5))%5 = (5+4-(2%5))%5 = 2
    
这将帮助您避免对
vow[]
数组进行负面索引

//when j=0 i.e, vowel `a`
(5+j- (V%5))%5 = (5+0-(2%5))%5 = 3

//when j=1 i.e, vowel `e`
(5+j- (V%5))%5 = (5+1-(2%5))%5 = 4

//when j=2 i.e, vowel `i`
(5+j- (V%5))%5 = (5+2-(2%5))%5 = 0    

//when j=3 i.e, vowel `o`
(5+j- (V%5))%5 = (5+3-(2%5))%5 = 1

//when j=4 i.e, vowel `u`
(5+j- (V%5))%5 = (5+4-(2%5))%5 = 2

还有一种方法是

  • 将值赋给
    V
    make
    V=(V%5)+5
  • 现在,您可以使用
    (V-j)%5
    作为索引
如果字符串只有元音i、o、u,则此代码有效,但如果字符串有a、e,则输出将错误:

  • 这是因为当元音是
    a
    e
    时,
    j-V%5
    是负数(因为,j是
    0
    1
    ),正如您正确地提到的

  • 只需使用
    (5+j-(V%5))%5
    即可避免
    vow[]
    数组的负索引值

    //when j=0 i.e, vowel `a`
    (5+j- (V%5))%5 = (5+0-(2%5))%5 = 3
    
    //when j=1 i.e, vowel `e`
    (5+j- (V%5))%5 = (5+1-(2%5))%5 = 4
    
    //when j=2 i.e, vowel `i`
    (5+j- (V%5))%5 = (5+2-(2%5))%5 = 0    
    
    //when j=3 i.e, vowel `o`
    (5+j- (V%5))%5 = (5+3-(2%5))%5 = 1
    
    //when j=4 i.e, vowel `u`
    (5+j- (V%5))%5 = (5+4-(2%5))%5 = 2
    
这将帮助您避免对
vow[]
数组进行负面索引

//when j=0 i.e, vowel `a`
(5+j- (V%5))%5 = (5+0-(2%5))%5 = 3

//when j=1 i.e, vowel `e`
(5+j- (V%5))%5 = (5+1-(2%5))%5 = 4

//when j=2 i.e, vowel `i`
(5+j- (V%5))%5 = (5+2-(2%5))%5 = 0    

//when j=3 i.e, vowel `o`
(5+j- (V%5))%5 = (5+3-(2%5))%5 = 1

//when j=4 i.e, vowel `u`
(5+j- (V%5))%5 = (5+4-(2%5))%5 = 2

还有一种方法是

  • 将值赋给
    V
    make
    V=(V%5)+5
  • 现在,您可以使用
    (V-j)%5
    作为索引

括号中应该是V%5,因为如果V=70,输出将是错误的。或者我错了?在parenteses中是v%5,然后再次是%5我只是用v=2来修正这些想法,但它应该是一个通用数字。括号中应该是v%5,因为如果v=70,输出将是错误的。或者我错了?在parenteses中是v%5,然后再次是%5我只是用v=2来修正这些想法,但它应该是一个通用数字