Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to encode a string by incrementing each char a certain amount. I've try'd this:
C#
public string EncodeString(string s)
        {
            string en = "";
            char g = '0';
            char t = '0';
            for (int i = 0; i <= s.Length; i++)
            {
                g = s[i];
                t = int(g + 25);
                en = en + t;
            }
            return en;
        }
but it gives me an error: invalid expression term int
Posted
Updated 31-Jul-12 13:47pm
v2
Comments
Kenneth Haugland 31-Jul-12 19:07pm    
s.Length -1 :=)
Peter_in_2780 31-Jul-12 19:29pm    
You have a number of problems. The first is that strings are immutable in c#.

Try this:

C#
var c = '0';
var c2 = (char)(c + 1);


You can now use this concept with Linq to:

var s = new string("12345".Select(i => (char) (i + 25)).ToArray());
 
Share this answer
 
v2
C#
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
            {
                sb.Append(Convert.ToChar(s[i] + 25));
            }
            return sb.ToString();


I am assuming you want to increment each character by 25 and convert it back to ASCI string. If just want to increment each character in string by 25 remove Convert.ToChar() function.

If you pass 12345 to above code you get back JKLMN.
 
Share this answer
 

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