从键盘接收一个字符串,显示字符串的长度,并查找该串中是否存在字母A(无论大小写),若不存在,则将大写字母A插入到串首位置,并显示新串。用C#语言
//此题的C#语言的代码如下:
using System;
class HelloWorld {
static void Main() {
Console.WriteLine("input a string");
string rd = Console.ReadLine(), A="A";
int I,i;
I = rd.IndexOf( 'A', 0);
i = rd.IndexOf( 'a', 0);
//Console.WriteLine("a:{0}, A:{1}", i, I);
Console.WriteLine("the input string is:\n{0}\n", rd);
Console.WriteLine("its length is:\n{0}\n", rd.Length);
if (i<0 && I<0){ //没有字母a或A
rd = String.Concat(A, rd);
Console.WriteLine("the new string is:\n{0}", rd);
}
}
}
//编译调试结果1(含有字母a或A的情况):
//编译调试结果2(没有字母a或A的情况):
//代码如下(多发一次):
using System;
namespace StringApplication
{
class StringProg
{
static void Main(string[] args)
{
string[] starray = new string[]{"Down the way nights are dark",
"And the sun shines daily on the mountain top",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"};
string str = String.Join("\n", starray);
Console.WriteLine(str);
Console.ReadKey() ;
}
}
}