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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Oct 8, 2013, 8:37 PM EDT
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();
}
}
Veena SardaRank #79 · 24.5K reputationPosted Oct 8, 2013, 7:54 PM EDT
string output = "";
foreach( char c in input)
{
int unicode = c;
unicode++;
output += char.ConvertFromUtf32(unicode);
}
Console.WriteLine(output);
Console.ReadLine();
VulpesRank #55 · 29.2K reputationPosted Oct 9, 2013, 12:07 AM EDT
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();
}
}