Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi , i need help for large strings.The output is not correct. It needs to be in a alphabet.

What I have tried:

Python
s = input("enter a string here: ")
shift = int(input("enter a value here: "))
n = len(s)
output = []
i = 0
output = [(chr(ord(s[i])+(i+1)+shift)) for i in range(n)]
print(output)
Posted
Updated 15-Nov-20 7:38am
Comments
Richard MacCutchan 15-Nov-20 11:05am    
"The output is not correct."
What does it look like and why is it not correct?
Patrice T 15-Nov-20 12:49pm    
Show sample input with actual and expected output !

Quote:
for large strings.The output is not correct
Of course, it is not. You are adding the index i to the character code in order to obtain a new character code, but i, for large strings is, well..., large.
So, if you really need to add i the you have to 'put back' the character code in the allowed range (e.g. 'a',..,'z'). You need the modulo operator % for such a job.
 
Share this answer
 
v2
Carlo is right, but his suggestion doesn't go far enough.
If you need a string to be "in an alphabet" - by which you mean that the result should contain only the alphabetic characters a to z, A to Z, space, and some punctuation, then you need to be rather more clever than your existing code.

Character sets do not contain just alphabetic characters - the smallest character set is seven bits wide, and contains 128 characters (Basic ASCII), more normal usage is Extended ASCII with eight bits for 256 characters, and most systems these days use Unicode, which can contain between 8 and 32 bits per character for a truly monumental number of potential characters!

If you want to restrict input and output to a specific subset of any available character set then you need to create an array holding each valid character a, b, c, d, ... z, A, B, C, ... +, -, ?, space, 1, 2, 3, ... 0, !, ", ', ... and so on.
Then look up each character in the input in that array, get it's index, and modify that by adding your loop guard and shift values. Then use the modulus operator "%" to bring the range of values back within the scope of the array, and fetch the output character from there.

It sounds complicated, but it really isn't: try it on paper and you'll see what I mean!
Give it a try ... it's really pretty easy once you get your head around it.
 
Share this answer
 
Python
def encrypt(cäsar):
    shiftValue=int(input("enter a shift value"))
    string=len(cäsar)
    for i in cäsar: 
        if i.isupper():  
            print(chr((ord(i)+(i+1)+shiftValue-65)%26+65),end="") 
        else:
            print(chr((ord(i)+(i+1)+shiftValue-97)%26+97),end="") 
            
enter=input("enter a string ")
encrypt(enter)
TypeError: can only concatenate str (not "int") to str



I get all the time that error. Do i need to convert the 65 and 97 to strings?
 
Share this answer
 
Comments
Richard Deeming 16-Nov-20 3:50am    
This is not a "solution" to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900