C # number of tasks 1 and 2 in the fifth week of the machine and the reverse order

Source: Internet
Author: User

[Csharp]
/*
* Start the program header annotation.
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: number of occurrences and Reverse Order
* Author: Xue Guangchen
* Completion date: January 1, October 08, 2011
* Version No.: x1.0

* Description of tasks and Solutions
* Input description:
* Problem description: Write a class named MyClass and a method named CountChar in the class,
The return value is an integer and two parameters. The first parameter can be a string, an integer, a single precision, or a double precision,
The second parameter is a character, and the method function returns the number of times that the second parameter appears in the first parameter. For example, CountChar ("6221982", '2') returns 3.
 
Continue to write the method in this class, with the name Reconvert and one parameter,
But it can be a string, integer, single-precision, double-precision. The method function returns the reverse order of parameters. For example, Reconvert (6221982) returns 2891226
 
* Program output:
* End the comment in the program Header
*/
 
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
 
Namespace MyClass
{
Class Program
{
Static void Main (string [] args)
{
Console. Write ("enter a string :");
String str = Console. ReadLine ();
 
Console. Write ("enter a character :");
Char ch = Console. ReadKey (). KeyChar;
 
Int number = CountChar (str, ch );
Console. WriteLine ("\ n character {0} appears in string {1} {2} Times \ n", ch, str, number );
 
String str1 = Reconvert (str );
Console. Write ("the reverse order of {0} is {1} \ n", str, str1 );
Console. ReadKey ();
}
 
Static int CountChar (string str, char ch)
{
Int num = 0;
For (int I = 0; I <str. Length; I ++)
{
If (str [I] = ch)
{
Num ++;
}
}
Return num;
}
 
Static string Reconvert (string str)
{
Char [] ch = new char [str. Length];
String str1 = "";
For (int I = str. Length-1, j = 0; I> = 0; I --, j ++)
{
Ch [j] = str [I];
Str1 + = ch [j]. ToString ();
}

Return str1;
}
}
}

Running result:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.