in c# how to program next letter in a word
in c# how to program when "cad " is given , i should get the output as "dbe" that is.,next letter of given word...how to do it?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjeeb LenkaPosted Oct 9, 2013, 12:37 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Nextletter
{
static void Main()
{
string input = "cad";
string output = "";
foreach (char c in input)
{
output += (char)((int)c + 1);
}
Console.WriteLine(output);
Console.ReadLine();
}
}
VulpesPosted Oct 9, 2013, 4:07 AM
In the former case, I suggest you 'wrap round' back to 'a' and in the latter case, leave the character unaltered:
using System;
class Program
{
static void Main()
{
Console.Write("Input a word : ");
string word = Console.ReadLine();
char[] chars = word.ToCharArray();
for(int i = 0; i < chars.Length; i++)
{
if (!Char.IsLetter(chars[i])) continue;
if (chars[i] == 'z' || chars[i] == 'Z')
{
chars[i] = (char)(chars[i] - 25);;
}
else
{
chars[i] = (char)(chars[i] + 1);
}
}
Console.WriteLine("The result is : {0}", new string(chars));
Console.ReadKey();
}
}
Veena SardaPosted Oct 8, 2013, 11:54 PM
string output = "";
foreach( char c in input)
{
int unicode = c;
unicode++;
output += char.ConvertFromUtf32(unicode);
}
Console.WriteLine(output);
Console.ReadLine();