Hi I'm now into data structures:
this is the problem:
string s = "abcdef"
char[] mychar = string.TocharArray();
int Length = s.length;
StringBuilder sb = new StringBuilder(Length);
-------Basically I need to construt a new string with the last two character iterated twice if the string = the same string.
does anybody now how?
the result would be "abcdgh"---> note the last two character are iterated twice

Recommended Answers

All 4 Replies

Sorry, seems I didn't understand you well, you need to get last two characters and increment their value (means if input string xxxxAB so the result should be xxxxBC)?
if yes get last 2 characters get each of which increment thier ascii value by 1. that's my solution, if you need code reply again.

I've done done that my problem is that I cant preppend the previous characters in the buffer this is my solution:
--------------------
string input = "abcdef";
char[] mychar = input.ToCharArray();
int length = input.Length;
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++ )
sb.Append((char)((int)mychar + 2));
Console.WriteLine(sb.ToString());
Console.ReadLine();
--------------------
However this souliton iterates each character twice and I just want the last two

actually I figuree out but is there another shorter way to doi it :
string input = "mystring";
char[] mychar = input.ToCharArray();
int length = input.Length ;
string newinput = input.Substring(0, input.Length -2);
StringBuilder sb = new StringBuilder(length);
for (int i = length - 2; i < length; i++)
sb.Append((char)((int)mychar + 2));
Console.WriteLine(newinput + sb.ToString());

Hi,
Try following code.

class Program
{
    static void Main(string[] args)
    {
        string temp = "abcdef";
        char[] temp1 = new char[temp.Length];
        temp1 = temp.ToCharArray();
        for (int i = temp.Length-2;i<temp.Length;i++)
        {
            temp1[i] = Convert.ToChar((Convert.ToInt32(temp1[i]) + 2));
        }
        temp = new string(temp1);
    }
}

Finally the string " temp " contains the result you want.

Please feel free to ask me if you have any queries and let me know the code is working correctly or not.

Regards,
Vijay Bhaskar

Hi I'm now into data structures:
this is the problem:
string s = "abcdef"
char[] mychar = string.TocharArray();
int Length = s.length;
StringBuilder sb = new StringBuilder(Length);
-------Basically I need to construt a new string with the last two character iterated twice if the string = the same string.
does anybody now how?
the result would be "abcdgh"---> note the last two character are iterated twice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.