c - How to avoid counting the non-alphanumeric characters -
i making crypting program cs50x , have small problem. trying crypt text using vigenere cipher(cipher uses word key). problem have that, whenever have space or non-alphabetic character, keeps incrementing. per spec, shouldn't... therefore, characters getting using wrong key... idea how can solve problem?
here small part of code understand problem.
for(int x=0, = 0 , textlen = strlen(text); x < textlen; x++ , i++ ) if (isalpha(text[x])) { if (islower(keyword[i % num_letters])) { if (islower(text[x]) && ((text[x] + (keyword[i % num_letters]) - 'a') <= 'z')) { text[x]= (text[x] + (keyword[i % num_letters]) - 'a' ); } else if (islower(text[x]) && ((text[x] + (keyword[i % num_letters]) - 'a') > 'z' )) { text[x]= ('a'+ ((((text[x]) - 'a') + ((keyword[i % num_letters]) - 'a' )) % 26)); } } printf("%c", text[x]); } else // print non-alpha char { printf("%c", text[x]); } i hope piece of code understand. fragment code should enough understand problem , perhaps suggest solution.
thanks!
this may weird way solve it, think simple solution be, in else statement, decrement value. problem regardless of whether entering if/else statement, both finish , return top, incrementing if nothing changed. more readable solution take control of variable away loop , increment right before/after print char
Comments
Post a Comment