当前位置:网站首页>C method question 1
C method question 1
2022-07-07 23:34:00 【Bobo in summer】
1. Palindrome refers to a string that reads both forward and backward . Write a method , Judging a string str1, Is it a palindrome , Is a palindrome that returns true, Otherwise return to false. Like strings b yes ag7ga It's palindrome. , And strings abc6es It's not palindrome . Require application writing , To test the correctness of the method .
public static bool huiwen(string s)
{
bool a;
string str = null;
for (int i = s.Length - 1; i >= 0; i--)
{
str += s[i];
}
if (str == s)
{
a = true;
}
else
a = false;
return a;
}
string s = Console.ReadLine();
bool a = huiwen(s);
if (a == true)
Console.WriteLine(" It's palindrome. ");
else
Console.WriteLine(" Not a palindrome ");
Console.ReadLine();

2. Write a method , Count the number of words in a string , The return value is the number of words . Specify that words are separated by several spaces . For example, if you enter a string " I am a student ", The result is 4. Require application writing , To test the correctness of the method .
public static int words(string s)
{
int count = 1;
for (int i = 0; i < s.Length; i++)
{
*****if (s[i] == ' ' && s[i + 1] != ' ')*****
{
count++;
}
}
return count;
}
string s = Console.ReadLine();
int a = words(s);
Console.WriteLine(" The number of words is {0}", a);
Console.ReadLine();

3. Write a method , Judge whether a positive integer is a prime number , The return value is of boolean type . Require application writing , seek 1-100 All the prime Numbers between .
public static bool sushu(int a)
{
for (int i = 2; i < a; i++)
{
if (a % i == 0)
{
return false;
}
}
return true;
}
public static void Main(string[] args)
{
int aaa =int.Parse(Console.ReadLine());
if (sushu(aaa) == true)
{
Console.WriteLine(" Prime number ");
}
else
Console.WriteLine(" Not primes ");
for (int i = 1; i < 100; i++)
{
if (sushu(i) == true)
Console.WriteLine(i);
}

4. Enter a string , Statistics string Chinese and English letters 、 Number of numeric characters and other symbols and output . Require application writing , To test the correctness of the method .
static void Letterdigitfuhao(string st)
{
string ssletter = "";
string ssdigit = "";
string ssfuhao = "";
int letter = 0;
int digit = 0;
int fuhao = 0;
for (int i = 0; i < st.Length; i++)
{
***if (char.IsLetter(st[i]))***
{
letter++;
ssletter += st[i];
}
else if (char.IsDigit(st[i]))
{
digit++;
ssdigit += st[i];
}
else
{
fuhao++;
ssfuhao += st[i];
}
}
Console.WriteLine(" The number of letters is {0} individual , Among them is {1}", letter, ssletter);
Console.WriteLine(" The number is {0} individual , Among them is {1}", digit, ssdigit);
Console.WriteLine(" The number of other symbols is {0} individual , Among them is {1}", fuhao, ssfuhao);
}
public static void Main(string[] args)
{
Console.WriteLine(" Please enter a string :");
string st = Console.ReadLine();
Letterdigitfuhao(st);
Console.ReadLine();
}

5. Write a method , Right integer m Sum up , The summation formula is s= 1/2+1/3+…+1/m, Method returns s Value . Require application writing , To test the correctness of the method .
public static double ssum(double n)
{
double sum = 0;
for (double i = 1; i < n; i++)
{
sum += 1 / (i + 1);
}
return sum;
}
double aaa =double.Parse( Console.ReadLine());
Console.WriteLine(ssum(aaa));

public static string AddDollar(string sentence)
{
string st = string.Empty;
for (int i = 0; i < sentence.Length; i++)
{
***if (char.IsLetter(sentence[i]))***
{
st += sentence[i] + "$";
}
else
{
st += sentence[i];
}
}
return st;
}
string st = Console.ReadLine();
Console.WriteLine(" Add ¥ The result is {0}", AddDollar(st));

6. Write a method , Add one after all English characters in a string $ character , And return the processed string . For example, the input :A1B23CD45, The return value of the method is :A$1B 23 C 23C 23CD$45, Require application writing , To test the correctness of the method .
public static string AddDollar(string sentence)
{
string st = string.Empty;
for (int i = 0; i < sentence.Length; i++)
{
***if (char.IsLetter(sentence[i]))***
{
st += sentence[i] + "$";
}
else
{
st += sentence[i];
}
}
return st;
}
public static void Main(string[] args)
{
Console.WriteLine(" Please enter a string :");
string st = Console.ReadLine();
Console.WriteLine(" Add ¥ The result is {0}", AddDollar(st));
}

7. Write a method , Delete all lowercase characters in the string , The rest of the characters remain unchanged . Method returns the converted String .str=“AbC” Change to string to =“AC”, Require application writing , To test the correctness of the method .
public static string Dellower(string stt)
{
string st1 = string.Empty;
for (int i = 0; i < stt.Length; i++)
{
if (char.IsUpper(stt[i]))
{
st1 += stt[i];
}
}
return st1;
}
public static void Main(string[] args)
{
Console.WriteLine(" Please enter a string :");
string sttttt = Console.ReadLine();
Console.WriteLine(Dellower(sttttt));
Console.ReadLine();
}

8. Write a method , For a string , Encrypt according to the following rules : If it is an English letter, capital will change to lowercase 、 Lowercase to uppercase , For non English characters, it remains unchanged . The return value is to return the encrypted string . Require application writing , To test the correctness of the method .
public static string UpperLowerDigit(string stt)
{
string st = string.Empty;
for (int i = 0; i < stt.Length; i++)
{
if (char.IsUpper(stt[i]))
{
st += char.ToLower(stt[i]);
}
else if (char.IsLower(stt[i]))
{
st += char.ToUpper(stt[i]);
}
else if (char.IsDigit(stt[i]))
{
st += stt[i];
}
}
return st;
}
public static void Main(string[] args)
{
Console.WriteLine(" Please enter a string :");
string st = Console.ReadLine();
Console.WriteLine(UpperLowerDigit(st));
Console.ReadLine();
}

9. Write a method , Find two integers m and n Maximum common divisor of , And return... As a return value . Require application writing , To test the correctness of the method .
public static int Gongyushu(int m, int n)
{
int gys = 0;
int temp = 0;
if (m < n)
{
temp = m;
m = n;
n = temp;
}
***for (int i = n; i > 0; i--)***
{
***if (m % i == 0 && n % i == 0)***
{
gys = i;
break;
}
}
return gys;
}
public static void Main(string[] args)
{
Console.WriteLine(" Find two integers m and n Maximum common divisor of ");
int m = int.Parse(Console.ReadLine());
int n = int.Parse(Console.ReadLine());
Console.WriteLine(Gongyushu(m, n));
Console.ReadLine();
}

10. Write a method , Find two integers m and n The least common multiple of , And return... As a return value . Require application writing , To test the correctness of the method .
public static int Gongbeishu(int m, int n)
{
int gys = 0;
int temp = 0;
if (m < n)
{
temp = m;
m = n;
n = temp;
}
for (int i = m; i <= m * n; i++)
{
if (i % m == 0 && i % n == 0)
{
gys = i;
break;
}
}
return gys;
}
public static void Main(string[] args)
{
Console.WriteLine(" Find two integers m and n Maximum common divisor of ");
int m = int.Parse(Console.ReadLine());
int n = int.Parse(Console.ReadLine());
Console.WriteLine(Gongbeishu(m, n));
Console.ReadLine();
}

11. Write a method , seek s=1/a+1/aa+1/aaa+1/aaaa+1/aa…a Value , among a Is a user-defined number . for example 1/2+1/22+1/222+1/2222+1/22222( At this time, there is 5 Add up the numbers ), The return values are and s. Require application writing , To test the correctness of the method .
***public static double Aaaaa(double aa, int nn)***
{
double s = 0;
double b = 1;
for (int i = 1; i < nn; i++)
{
b = b * 10 + 1;
s += 1 / (b * aa);
}
return s + 1 / aa;
}
public static void Main(string[] args)
{
Console.WriteLine(" Please enter a number a(a yes 1-9 Number between ):");
double aaa = double.Parse(Console.ReadLine());
Console.WriteLine(" frequency :");
int nnnn = int.Parse(Console.ReadLine());
Console.WriteLine(Aaaaa(aaa, nnnn));
Console.ReadLine();
}

12. Write a method , Judge whether a number is perfect , The return value is of boolean type . If a number is exactly equal to the sum of its factors , This number is called “ Complete ”. for example 6=1+2+3. Require application writing , To test the correctness of the method .
public static bool wanshu(double n)
{
double sum = 0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
sum += i;
}
}
if (sum == n)
return true;
else
return false;
}
double aaa = double.Parse(Console.ReadLine());
Console.WriteLine(wanshu(aaa));

边栏推荐
- How to login and enable synchronization function in Google browser
- Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
- leetcode-520. Detect capital letters -js
- LeeCode -- 6. Zigzag transformation
- As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
- PCI-Express接口的PCB布线规则
- Ros2 topic (03): the difference between ros1 and ros2 [01]
- Lm12 rolling heikin Ashi double K-line filter
- 高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
- 648. Word replacement
猜你喜欢

Map operation execution process

What if once again forgets the login password of raspberry pie? And you don't have a monitor yet! Today, I would like to introduce a method

Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
![给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」

Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting

re1攻防世界逆向
![Ros2 topic (03): the difference between ros1 and ros2 [02]](/img/12/244ea30b5b141a0f47a54c08f4fe9f.png)
Ros2 topic (03): the difference between ros1 and ros2 [02]

Live-Server使用

Matlab SEIR infectious disease model prediction

进度播报|广州地铁七号线全线29台盾构机全部完成始发
随机推荐
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
HDU 4747 Mex「建议收藏」
Vs extension tool notes
LDO voltage stabilizing chip - internal block diagram and selection parameters
云原生正在吞噬一切,开发者该如何应对?
Anxin vb01 offline voice module access intelligent curtain guidance
[untitled]
SAP 内存参数调优过程
欢聚时代一面
B_ QuRT_ User_ Guide(36)
Home appliance industry channel business collaboration system solution: help home appliance enterprises quickly realize the Internet of channels
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
Lm12 rolling heikin Ashi double K-line filter
FreeLink开源呼叫中心设计思想
Unity3d learning notes 4 - create mesh advanced interface
PHP uses Alibaba cloud storage
Explain
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
One week learning summary of STL Standard Template Library