Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to pass if input string is "A" means i need to get next leve as "B". If input value is "B" means "C".
E.g:
string input1 = "A";
string input2 = "B";
output:
input1 ---> B
input2 ---> C
How to do it in programmatically. Please give me a solution to do this.
This is the best as far as i understand the problem:
public char NextLevel(char c)
{
if (c == 'Z')
return 'A';
return (char)(c + 1);
}
private string NextChar(string character)
{
if (character == null) throw new ArgumentNullException(nameof(character));
if (character.Length != 1) throw new ArgumentOutOfRangeException(nameof(character), "You can use only a single letter string");
return Convert.ToString(NextChar(character[0]));
}
private char NextChar(char character)
{
return (char)(character + 1);
}
If you need more details you can use this link: https://github.com/jenseralmeida/stackoverflow/blob/q37608162/src/LibraryTests/DraftTests.cs
This should work for you.
public string NextAlphaString(string x)
{
if(x == "z" || x == "Z")
return x.Replace(x[0],(char)(x[0] - 25));
else
return x.Replace(x()[0],(char)(x[0] + 1));
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I want to find out how I could detect if a string contains only one type of letter for example
Input:
......
I want it to detect if the string contains only that letter, so the output should be something like this
String contains only one type of letter
Try this:
private bool ContainsOnlyOneLetter(string String)
{
if(String.Length == 0)
{
return true;
}
for(int i = 0; i < String.Length;i++)
{
if(String.Substring(i,1) != String.Substring(0,1))
{
return false;
}
}
return true;
}
And you can use the function like this:
bool containsOneLetter = ContainsOnlyOneLetter("...");
if (containsOneLetter == true)
{
//Put code here when the letters are the same...
}
else
{
//Code when there are different letters..
}
This regular expression
^(\p{L})(\1)*$
matches strings where
The 1st character is a Unicode Letter (a character class covering many, many more characters than just the ASCII characters A-Z and a-Z), followed by,
Zero or more repetitions of _the exact same character matched by the first group
So the empty string would fail the test, as would "aaaaaAaaa", but "aaaaaaa" would pass the test.
But... why?
This is arguably simpler, and almost certainly faster than the above regular expression:
public static bool isAllSameCharacter( string s )
{
bool isValid = s != null && s.length > 0 && s[0].isLetter();
char firstChar = s[0];
for ( int i = 1 ; isValid && i < s.Length ; ++i )
{
isValid = s[i] == firstChar;
}
return isValid;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
using System;
namespace Let_sTalk
{
class Program
{
static void Main(string[] args)
{
Console.Write("<-:");
string q = Console.ReadLine();
string w = Console.ReadLine();
string e = Console.ReadLine();
if (q, w, e == "/")
{
Console.WriteLine("over");
}
}
}
}
You cannot use the comma (,) symbol to separate variables in an if statement. That's invalid C# syntax.
Also, in C# string comparison is done with the Equals function. The == will still work in most cases, but there are some additional error handlings with the Equals function.
If you want to check if at least one of the q, w, e variables is equal to /, then use the following code:
if (q.Equals("/") || w.Equals("/") || e.Equals("/"))
{
Console.WriteLine("over");
}
If you wish to check if all q, w, e variables are equal to /, then you have to replace the || (or operator) with && (and operator) like so:
if (q.Equals("/") && w.Equals("/") && e.Equals("/"))
{
Console.WriteLine("over");
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I was wondering if anyone could please tell me a nicer way of writing this. These are the only conditions that are used.
private static void BondedNonBoundedIndicator(InvoiceLine invoiceLine, Product packageProduct)
{
var value = packageProduct.BondedQuantity;
var value2 = packageProduct.NonBondedQuantity;
if (value == 0 && value2 == 1)
{
invoiceLine.BondedORBbondedIndicator = "N";
}
if (value == 1 && value2 == 0)
{
invoiceLine.BondedORBbondedIndicator = "Y";
}
}
Using C# 8, we can use tuples and the switch expression to simplify this:
invoiceLine.BondedORBbondedIndicator = (value, value2) switch {
(0, 1) => "N",
(1, 0) => "Y",
_ => invoiceLine.BondedORBbondedIndicator
};
You could shorten the last case even further if you know the invoiceLine.BondedORBbondedIndicator before the code is run is always, say ""(, then the last case would be _ => "").
You can use xor logic
If(value1^value2 == 1)
value1 == 0 ? invoiceLine.BondedORBbondedIndicator = "N" : invoiceLine.BondedORBbondedIndicator = "Y";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
so i'm trying to make a program (text based) that can scan a word you type into it to see if it's a palindrome or not so just a true or false outcome i'm a beginner with c# so please help me out!
So far this is all i have done.
//Name space
namespace PalindromeChecker
{
//Class
class Program
{
//Function
static void Main(string[] args)
{
Console.WriteLine("Type the word");
ConsoleKeyInfo Keyinfo = Console.Readkey();
}
}
}
A palindrome is a sequence of characters that is the same whether read forward or in in reverse. So to determine if a string is a palindrome, we can compare the string with its reverse, it is one.
So how can you create a reverse of a string? If you look at the String definition, you'll see it implements IEnumerable. Now the System.Linq namespace offers the IEnumerable.Reverse method. This can be used to reverse a string:
var str = "oxo";
var reversed = str.Reverse();
However, this returns an enumeration of char, not a string. So string.Concat is needed to turn reversed into a string. Having done that, we now have:
var str = "oxo";
var reversed = string.Concat(str.Reverse());
To test if they are the same, simply compare them:
if (str == reversed)
{
// we have a palindrome
}
A "gotcha here is that if str is null, you'll get a null exception with str.Reverse. That can be handled with a null check. So we can then simplify the whole thing down to:
if (str != null && str == string.Concat(str.Reverse()))
{
// we have a palindrome
string word = "hi"; ;
char[] wordArray = word.ToCharArray();
char[] revWordArray = wordArray.Reverse().ToArray();
int count = 0;
for (int i1 = 0; i1 < wordArray.Length; i1++)
{
if (wordArray[i1] == revWordArray[i1])
{
count++;
}
}
if (count == wordArray.Length)
{
bool a = true;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having some trouble turning the following code into a for loop. The purpose of the code is to check if the string has both a letter and a number in it.
else if (!string.Any(Char.IsLetter) || !string.Any(Char.IsDigit))
{
return false;
}
Any ideas?
Do you mean something like this?
bool anyLetter = false;
bool anyDigit = false;
foreach(var ch in str)
{
if(char.IsLetter(ch)) anyLetter = true;
if(char.IsDigit(ch)) anyDigit = true;
if(anyLetter && anyDigit) break;
}
return anyLetter || anyDigit;
Note that if this string should contain at least one digit and one letter, you need to use && instead of ||
Since Selman22 seems to have already answered the question, another solution I found is I guess you could also use RegEx:
letterCount = Regex.Matches(yourstring,#"[a-zA-Z]").Count;
numberCount = Regex.Matches(yourstring,#"\d").Count;
return letterCount != 0 && numberCount != 0;
Did you mean a loop for a set of strings?
var src = new List<string>{"A4C", "D3F", "G7I"};
var allHaveCharAndDigit = src.TrueForAll(s => s.Any(c => char.IsLetter(c)) && s.Any(c => char.IsDigit(c)));