2.1 determine which character a char contains, 2.1char

Source: Internet
Author: User
Tags strfind

2.1 determine which character a char contains, 2.1char
Knowledge point:

1. char. IsControl

2. char. IsPunctuation

3. char. IsSurrogate

4. char. IsWhitespace

5. char. IsDigit

6. char. IsNumber

7. char. IsSeparation

8. char. IsSymbol

 

Problem:

There is a char type variable that you want to determine whether it contains letters, digits, numbers, punctuation marks, control characters, delimiters, blank spaces, or replacement characters. Similarly, a string variable may be used to determine the character at one or more positions in the string.

Solution

To determine a char value, you can use the built-in static method of the System. Char structure, as shown below:

1. char. IsControl

2. char. IsPunctuation

3. char. IsSurrogate

4. char. IsWhitespace

5. char. IsDigit

6. char. IsNumber

7. char. IsSeparation

8. char. IsSymbol

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace _ 02 determine the characters in a Char {class Program {static void Main (string [] args) {Console. writeLine ("enter a character:"); string symbol = Console. readLine (); CharKind ck = GetCharKind (Convert. toChar (symbol); Console. writeLine (ck); string symbol1 = Console. readLine (); int position = Convert. toInt32 (Console. readLine (); CharKind ck1 = GetCharKindInString (symbol1, position); Console. writeLine (ck1); Console. readKey ();} public static CharKind GetCharKind (char theChar) {if (char. isLetter (theChar) {return CharKind. letter;} else if (char. isNumber (theChar) {return CharKind. number;} else if (char. isPunctuation (theChar) {return CharKind. punctuation;} else {return CharKind. unknown;} // determines the character type of a position in the string. public static CharKind GetCharKindInString (string theString, int CharPosition) {if (char. isLetter (theString, CharPosition) {return CharKind. letter;} else if (char. isNumber (theString, CharPosition) {return CharKind. number;} else if (char. isPunctuation (theString, CharPosition) {return CharKind. punctuation;} else {return CharKind. unknown ;}} public enum CharKind {Letter, Number, Punctuation, Unknown }}View Code

 


Verification Result 1 8 Number
Java determines whether a String contains a character char. For example, it determines whether "abcde" contains 'C '.

Print by yourself ~
String str = "abcd ";
If (str. indexOf (String. valueOf ('C') =-1 ){
// The string does not contain c
} Else {
// The String contains c, which is in the str. indexOf (String. valueOf ('C') position

Write a function char * strfind (char * s, char * t) in c ++ to find the position where string t appears for the first time in the string.

I personally think the char * of your return value is not reasonable, so I just modified it a little. Contact me if you have any questions. The program has been run!
# Include <iostream. h>
Int Strfind (char * s, char * t)
{
Int index, lengths = 0, lengtht = 0, I, j;
Char * Position;
While (s [lengths]! = 0) // obtain the length of string s
{
Lengths ++;
}
While (t [lengtht]! = 0)
{
Lengtht ++;
}
For (I = 0; I <lengths-lengtht; I ++) // cycle to the length of the source string minus the length of the target string.
{
Index = 0;
While (s [I + index] = t [index] & index <lengtht)
{
Index ++;
}
If (index = lengtht)
{
// Position = new char;
// * Position = I;
// Return Position;
Return I; // the position where I is returned.
}
}
// Position = NULL;
Return-1; // cannot be found
}
Int main ()
{
Char str1 [500];
Char str2 [500];
Cin> str1;
Cin> str2;
// Char * Position = Strfind (str1, str2 );
// Cout <* Position <endl;
Cout <Strfind (str1, str2) <endl;
Return 0;
}

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.