字符串的用法1
1、编写一个控制台应用程序,完成下列功能:
(1)从键盘接收一个字符串,输出该字符串的长度。
(2)统计该字符串中数字字符出现的次数,并显示每个数字字符的位置;若不存在数字字符,直接输出-1。
(3)将字符串中的字母字符按照原顺序输出其对应的大写字母;若不存在字母字符,直接输出-1。
(4)在字符串首部位置插入字串"C#",然后输出新字符串;
[样例输入]testabc
[样例输出]
7
-1
TESTABC
C#testabc
[样例输入]123abc
[样例输出]
6
3
0 1 2
ABC
C#123abc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入字符串:");
string str = Console.ReadLine();
int l = str.Length;
Console.WriteLine(l);
int countNum=0;
for (int i=0; i<l; i++)
{
if (char.IsNumber(str[i]))
{
countNum++;
}
}
if (countNum == 0)
{
Console.WriteLine("-1");
}
else
{
Console.WriteLine(countNum);
for (int i = 0; i < l; i++)
{
if (char.IsNumber(str[i]))
{
Console.Write(i + " ");
}
}
Console.WriteLine();
}
int countLet = 0;
for (int i = 0; i < l; i++)
{
if (char.IsLetter(str[i]))
{
countLet++;
}
}
if (countLet == 0)
{
Console.WriteLine("-1");
}
else
{
string s = str.ToUpper();
for (int i = 0; i < l; i++)
{
if (char.IsLetter(s[i]))
{
Console.Write(s[i]);
}
}
}
Console.WriteLine();
string news = str.Insert(0, "C#");
Console.WriteLine(news);
Console.ReadLine();
}
}
}