从键盘接收一个字符串,显示字符串的长度,并查找该串中是否存在字母A(无论大小写),若不存在,

从键盘接收一个字符串,显示字符串的长度,并查找该串中是否存在字母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() ;
      }
   }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-10-11
#include<stdio.h>
void main()
{
char str[256];
char c,x='A';
int i,k,n,t;
i=1;k=0; t=0;n=0;
printf("原字符串是:\n");
gets(str); //从键盘输入一个字符串,回车结束
t=strlen(str); //计算字符串长度
printf("字符串长度为:%d",t); //输出字符串长度
while ( str[i] )
{
k=i-1;
if ( str[i]=='a' || str[i]=='A') n++; //统计a/A出现次数
i++;

}
if(n==0) printf("字母a/A不存在,新字符串为:c%s%",x,str[i]);//若不存在,则输出带首字符为A的新字符串
else printf("字符a/A出现次数%d",n]); //若存在,则输出字符a/A出现次数
}本回答被网友采纳
相似回答