Csharp/C Sharp/Data Types/Char
Содержание
- 1 A queue class for characters
- 2 A set class for characters
- 3 A stack class for characters
- 4 Demonstrate several Char methods
- 5 Demonstrate the ICharQ interface: A character queue interface
- 6 Determining If A Character Is Within A Specified Range
- 7 Encode or decode a message
- 8 Escape Characters
- 9 Get char type: control, digit, letter, number, punctuation, surrogate, symbol and white space
- 10 Is a char in a range: Case Insensitive
- 11 Is a char in a range Exclusively
- 12 IsDigit, IsLetter, IsWhiteSpace, IsLetterOrDigit, IsPunctuation
- 13 Using Char
A queue class for characters
<source lang="csharp"> /* C# A Beginner"s Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295
- /
/*
Project 5-2 A queue class for characters.
- /
using System;
class Queue {
public char[] q; // this array holds the queue public int putloc, getloc; // the put and get indices public Queue(int size) { q = new char[size+1]; // allocate memory for queue putloc = getloc = 0; } // put a character into the queue public void put(char ch) { if(putloc==q.Length-1) { Console.WriteLine(" -- Queue is full."); return; } putloc++; q[putloc] = ch; } // get a character from the queue public char get() { if(getloc == putloc) { Console.WriteLine(" -- Queue is empty."); return (char) 0; } getloc++; return q[getloc]; }
}
// Demonstrate the Queue class. public class QueueDemo1 {
public static void Main() { Queue bigQ = new Queue(100); Queue smallQ = new Queue(4); char ch; int i; Console.WriteLine("Using bigQ to store the alphabet."); // put some numbers into bigQ for(i=0; i < 26; i++) bigQ.put((char) ("A" + i)); // retrieve and display elements from bigQ Console.Write("Contents of bigQ: "); for(i=0; i < 26; i++) { ch = bigQ.get(); if(ch != (char) 0) Console.Write(ch); } Console.WriteLine("\n"); Console.WriteLine("Using smallQ to generate erros."); // Now, use smallQ to generate some errors for(i=0; i < 5; i++) { Console.Write("Attempting to store " + (char) ("Z" - i)); smallQ.put((char) ("Z" - i)); Console.WriteLine(); } Console.WriteLine(); // more errors on smallQ Console.Write("Contents of smallQ: "); for(i=0; i < 5; i++) { ch = smallQ.get(); if(ch != (char) 0) Console.Write(ch); } }
}
</source>
A set class for characters
<source lang="csharp"> /* C# A Beginner"s Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295
- /
/*
Project 7-1 A set class for characters.
- /
using System;
class Set {
char[] members; // this array holds the set int len; // number of members // Construct a null set. public Set() { len = 0; } // Construct an empty set of a given size. public Set(int size) { members = new char[size]; // allocate memory for set len = 0; // no members when constructed } // Construct a set from another set. public Set(Set s) { members = new char[s.len]; // allocate memory for set for(int i=0; i < s.len; i++) members[i] = s[i]; len = s.len; // number of members } // Implement read-only Length property. public int Length { get{ return len; } } // Implement read-only indexer. public char this[int idx]{ get { if(idx >= 0 & idx < len) return members[idx]; else return (char)0; } } /* See if an element is in the set. Return the index of the element or -1 if not found. */ int find(char ch) { int i; for(i=0; i < len; i++) if(members[i] == ch) return i; return -1; } // Add a unique element to a set. public static Set operator +(Set ob, char ch) { Set newset = new Set(ob.len + 1); // make a new set one element larger // copy elements for(int i=0; i < ob.len; i++) newset.members[i] = ob.members[i]; // set len newset.len = ob.len; // see if element already exists if(ob.find(ch) == -1) { // if not found, then add // add new element to new set newset.members[newset.len] = ch; newset.len++; } return newset; // return updated set } // Remove an element from the set. public static Set operator -(Set ob, char ch) { Set newset = new Set(); int i = ob.find(ch); // i will be -1 if element not found // copy and compress the remaining elements for(int j=0; j < ob.len; j++) if(j != i) newset = newset + ob.members[j]; return newset; } // Set union. public static Set operator +(Set ob1, Set ob2) { Set newset = new Set(ob1); // copy the first set // add unique elements from second set for(int i=0; i < ob2.len; i++) newset = newset + ob2[i]; return newset; // return updated set } // Set difference. public static Set operator -(Set ob1, Set ob2) { Set newset = new Set(ob1); // copy the first set // subtract elements from second set for(int i=0; i < ob2.len; i++) newset = newset - ob2[i]; return newset; // return updated set }
}
// Demonstrate the Set class. public class SetDemo {
public static void Main() { // construct 10-element empty Set Set s1 = new Set(); Set s2 = new Set(); Set s3 = new Set(); s1 = s1 + "A"; s1 = s1 + "B"; s1 = s1 + "C"; Console.Write("s1 after adding A B C: "); for(int i=0; i<s1.Length; i++) Console.Write(s1[i] + " "); Console.WriteLine(); s1 = s1 - "B"; Console.Write("s1 after s1 = s1 - "B": "); for(int i=0; i<s1.Length; i++) Console.Write(s1[i] + " "); Console.WriteLine(); s1 = s1 - "A"; Console.Write("s1 after s1 = s1 - "A": "); for(int i=0; i<s1.Length; i++) Console.Write(s1[i] + " "); Console.WriteLine(); s1 = s1 - "C"; Console.Write("s1 after a1 = s1 - "C": "); for(int i=0; i<s1.Length; i++) Console.Write(s1[i] + " "); Console.WriteLine("\n"); s1 = s1 + "A"; s1 = s1 + "B"; s1 = s1 + "C"; Console.Write("s1 after adding A B C: "); for(int i=0; i<s1.Length; i++) Console.Write(s1[i] + " "); Console.WriteLine(); s2 = s2 + "A"; s2 = s2 + "X"; s2 = s2 + "W"; Console.Write("s2 after adding A X W: "); for(int i=0; i<s2.Length; i++) Console.Write(s2[i] + " "); Console.WriteLine(); s3 = s1 + s2; Console.Write("s3 after s3 = s1 + s2: "); for(int i=0; i<s3.Length; i++) Console.Write(s3[i] + " "); Console.WriteLine(); s3 = s3 - s1; Console.Write("s3 after s3 - s1: "); for(int i=0; i<s3.Length; i++) Console.Write(s3[i] + " "); Console.WriteLine("\n"); s2 = s2 - s2; // clear s2 s2 = s2 + "C"; // add ABC in reverse order s2 = s2 + "B"; s2 = s2 + "A"; Console.Write("s1 is now: "); for(int i=0; i<s1.Length; i++) Console.Write(s1[i] + " "); Console.WriteLine(); Console.Write("s2 is now: "); for(int i=0; i<s2.Length; i++) Console.Write(s2[i] + " "); Console.WriteLine(); Console.Write("s3 is now: "); for(int i=0; i<s3.Length; i++) Console.Write(s3[i] + " "); Console.WriteLine(); }
}
</source>
A stack class for characters
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// A stack class for characters.
using System;
class Stack {
// these members are private char[] stck; // holds the stack int tos; // index of the top of the stack // Construct an empty Stack given its size. public Stack(int size) { stck = new char[size]; // allocate memory for stack tos = 0; } // Construct a Stack from a stack. public Stack(Stack ob) { // allocate memory for stack stck = new char[ob.stck.Length]; // copy elements to new stack for(int i=0; i < ob.tos; i++) stck[i] = ob.stck[i]; // set tos for new stack tos = ob.tos; } // Push characters onto the stack. public void push(char ch) { if(tos==stck.Length) { Console.WriteLine(" -- Stack is full."); return; } stck[tos] = ch; tos++; } // Pop a character from the stack. public char pop() { if(tos==0) { Console.WriteLine(" -- Stack is empty."); return (char) 0; } tos--; return stck[tos]; } // Return true if the stack is full. public bool full() { return tos==stck.Length; } // Return true if the stack is empty. public bool empty() { return tos==0; } // Return total capacity of the stack. public int capacity() { return stck.Length; } // Return number of objects currently on the stack. public int getNum() { return tos; }
}
// Demonstrate the Stack class. public class StackDemo1 {
public static void Main() { Stack stk1 = new Stack(10); char ch; int i; // Put some characters into stk1. Console.WriteLine("Push A through Z onto stk1."); for(i=0; !stk1.full(); i++) stk1.push((char) ("A" + i)); // Create a copy of stck1 Stack stk2 = new Stack(stk1); // Display the contents of stk1. Console.Write("Contents of stk1: "); while( !stk1.empty() ) { ch = stk1.pop(); Console.Write(ch); } Console.WriteLine(); Console.Write("Contents of stk2: "); while ( !stk2.empty() ) { ch = stk2.pop(); Console.Write(ch); } Console.WriteLine("\n"); }
}
</source>
Demonstrate several Char methods
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// Demonstrate several Char methods.
using System;
public class CharDemo {
public static void Main() { string str = "This is a test. $23"; int i; for(i=0; i < str.Length; i++) { Console.Write(str[i] + " is"); if(Char.IsDigit(str[i])) Console.Write(" digit"); if(Char.IsLetter(str[i])) Console.Write(" letter"); if(Char.IsLower(str[i])) Console.Write(" lowercase"); if(Char.IsUpper(str[i])) Console.Write(" uppercase"); if(Char.IsSymbol(str[i])) Console.Write(" symbol"); if(Char.IsSeparator(str[i])) Console.Write(" separator"); if(Char.IsWhiteSpace(str[i])) Console.Write(" whitespace"); if(Char.IsPunctuation(str[i])) Console.Write(" punctuation"); Console.WriteLine(); } Console.WriteLine("Original: " + str); // Convert to upper case. string newstr = ""; for(i=0; i < str.Length; i++) newstr += Char.ToUpper(str[i]); Console.WriteLine("Uppercased: " + newstr); }
}
</source>
Demonstrate the ICharQ interface: A character queue interface
<source lang="csharp"> /* C# A Beginner"s Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295
- /
/*
Project 9-1 Demonstrate the ICharQ interface.
- /
using System; // A character queue interface. public interface ICharQ {
// Put a characer into the queue. void put(char ch); // Get a character from the queue. char get();
}
// A fixed-size queue class for characters.
class FixedQueue : ICharQ { char[] q; // this array holds the queue int putloc, getloc; // the put and get indices // Construct an empty queue given its size. public FixedQueue(int size) { q = new char[size+1]; // allocate memory for queue putloc = getloc = 0; } // Put a character into the queue. public void put(char ch) { if(putloc==q.Length-1) { Console.WriteLine(" -- Queue is full."); return; } putloc++; q[putloc] = ch; } // Get a character from the queue. public char get() { if(getloc == putloc) { Console.WriteLine(" -- Queue is empty."); return (char) 0; } getloc++; return q[getloc]; }
} // A circular queue. class CircularQueue : ICharQ {
char[] q; // this array holds the queue int putloc, getloc; // the put and get indices // Construct an empty queue given its size. public CircularQueue(int size) { q = new char[size+1]; // allocate memory for queue putloc = getloc = 0; } // Put a character into the queue. public void put(char ch) { /* Queue is full if either putloc is one less than getloc, or if putloc is at the end of the array and getloc is at the beginning. */ if(putloc+1==getloc | ((putloc==q.Length-1) & (getloc==0))) { Console.WriteLine(" -- Queue is full."); return; } putloc++; if(putloc==q.Length) putloc = 0; // loop back q[putloc] = ch; } // Get a character from the queue. public char get() { if(getloc == putloc) { Console.WriteLine(" -- Queue is empty."); return (char) 0; } getloc++; if(getloc==q.Length) getloc = 0; // loop back return q[getloc]; }
} // A dynamic queue. class DynQueue : ICharQ {
char[] q; // this array holds the queue int putloc, getloc; // the put and get indices // Construct an empty queue given its size. public DynQueue(int size) { q = new char[size+1]; // allocate memory for queue putloc = getloc = 0; } // Put a character into the queue. public void put(char ch) { if(putloc==q.Length-1) { // increase queue size char[] t = new char[q.Length * 2]; // copy elements into new queue for(int i=0; i < q.Length; i++) t[i] = q[i]; q = t; } putloc++; q[putloc] = ch; } // Get a character from the queue. public char get() { if(getloc == putloc) { Console.WriteLine(" -- Queue is empty."); return (char) 0; } getloc++; return q[getloc]; }
} // Demonstrate the queues. public class IQDemo {
public static void Main() { FixedQueue q1 = new FixedQueue(10); DynQueue q2 = new DynQueue(5); CircularQueue q3 = new CircularQueue(10); ICharQ iQ; char ch; int i; iQ = q1; // Put some characters into fixed queue. for(i=0; i < 10; i++) iQ.put((char) ("A" + i)); // Show the queue. Console.Write("Contents of fixed queue: "); for(i=0; i < 10; i++) { ch = iQ.get(); Console.Write(ch); } Console.WriteLine(); iQ = q2; // Put some characters into dynamic queue. for(i=0; i < 10; i++) iQ.put((char) ("Z" - i)); // Show the queue. Console.Write("Contents of dynamic queue: "); for(i=0; i < 10; i++) { ch = iQ.get(); Console.Write(ch); } Console.WriteLine(); iQ = q3; // Put some characters into circular queue. for(i=0; i < 10; i++) iQ.put((char) ("A" + i)); // Show the queue. Console.Write("Contents of circular queue: "); for(i=0; i < 10; i++) { ch = iQ.get(); Console.Write(ch); } Console.WriteLine(); // Put more characters into circular queue. for(i=10; i < 20; i++) iQ.put((char) ("A" + i)); // Show the queue. Console.Write("Contents of circular queue: "); for(i=0; i < 10; i++) { ch = iQ.get(); Console.Write(ch); } Console.WriteLine("\nStore and consume from" + " circular queue."); // Use and consume from circular queue. for(i=0; i < 20; i++) { iQ.put((char) ("A" + i)); ch = iQ.get(); Console.Write(ch); } }
}
</source>
Determining If A Character Is Within A Specified Range
<source lang="csharp"> using System; using System.Data; using System.Text.RegularExpressions; using System.Text; class Class1{
static void Main(string[] args){ Console.WriteLine(IsInRange("c", "a", "G")); Console.WriteLine(IsInRange("c", "a", "g")); Console.WriteLine(IsInRange("c", "c", "g")); Console.WriteLine(IsInRange((char)32, "a", "b")); } public static bool IsInRange(char testChar, char startOfRange, char endOfRange) { if (testChar >= startOfRange && testChar <= endOfRange) { // testChar is within the range return (true); } else { // testChar is NOT within the range return (false); } }
}
</source>
Encode or decode a message
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// Encode or decode a message.
using System;
public class Cipher {
public static int Main(string[] args) { // see if arguments a present if(args.Length < 2) { Console.WriteLine("Usage: encode/decode word1 [word2...wordN]"); return 1; // return failure code } // if args present, first arg must be encode or decode if(args[0] != "encode" & args[0] != "decode") { Console.WriteLine("First arg must be encode or decode."); return 1; // return failure code } // encode or decode message for(int n=1; n < args.Length; n++) { for(int i=0; i < args[n].Length; i++) { if(args[0]=="encode") Console.Write((char) (args[n][i] + 1) ); else Console.Write((char) (args[n][i] - 1) ); } Console.Write(" "); } Console.WriteLine(); return 0; }
}
</source>
Escape Characters
<source lang="csharp"> /*
* C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */
using System; namespace Client.Chapter_1___Common_Type_System {
public class EscapeCharacters { static void Main(string[] args) { char MyChar = "\0"; string MyString = @"C:\MyFiles"; string MYString2 = "c:\\Program Files"; string MyString3 = " \"To Be or Not To Be, That Is The Question\" "; } }
}
</source>
Get char type: control, digit, letter, number, punctuation, surrogate, symbol and white space
<source lang="csharp"> using System; using System.Data; using System.Text.RegularExpressions; using System.Text; class Class1{
static void Main(string[] args){ Console.WriteLine(GetCharKind("f")); Console.WriteLine(GetCharKind("0")); Console.WriteLine(GetCharKind(".")); Console.WriteLine(GetCharKind("}")); } public static String GetCharKind(char theChar) { if (Char.IsControl(theChar)) { return "Control"; } else if (Char.IsDigit(theChar)) { return "Digit"; } else if (Char.IsLetter(theChar)) { return "Letter"; } else if (Char.IsNumber(theChar)) { return "Number"; } else if (Char.IsPunctuation(theChar)) { return "Punctuation"; } else if (Char.IsSeparator(theChar)) { return "Separator"; } else if (Char.IsSurrogate(theChar)) { return "Surrogate"; } else if (Char.IsSymbol(theChar)) { return "Symbol"; } else if (Char.IsWhiteSpace(theChar)) { return "Whitespace"; } else { return "Unknown"; } }
}
</source>
Is a char in a range: Case Insensitive
<source lang="csharp"> using System; using System.Data; using System.Text.RegularExpressions; using System.Text; class Class1{
static void Main(string[] args){ Console.WriteLine(IsInRangeCaseInsensitive("c", "a", "G")); Console.WriteLine(IsInRangeCaseInsensitive("c", "a", "c")); Console.WriteLine(IsInRangeCaseInsensitive("c", "g", "g")); Console.WriteLine(IsInRangeCaseInsensitive((char)32, "a", "b")); } public static bool IsInRangeCaseInsensitive(char testChar, char startOfRange, char endOfRange) { testChar = char.ToUpper(testChar); startOfRange = char.ToUpper(startOfRange); endOfRange = char.ToUpper(endOfRange); if (testChar >= startOfRange && testChar <= endOfRange) { // testChar is within the range return (true); } else { // testChar is NOT within the range return (false); } }
}
</source>
Is a char in a range Exclusively
<source lang="csharp"> using System; using System.Data; using System.Text.RegularExpressions; using System.Text; class Class1{
static void Main(string[] args){ Console.WriteLine(IsInRangeExclusive("c", "c", "g")); Console.WriteLine(IsInRangeExclusive("c", "c", "g")); Console.WriteLine(IsInRangeExclusive((char)32, (char)31, "Z")); } public static bool IsInRangeExclusive(char testChar, char startOfRange, char endOfRange) { if (testChar > startOfRange && testChar < endOfRange) { // testChar is within the range return (true); } else { // testChar is NOT within the range return (false); } }
}
</source>
IsDigit, IsLetter, IsWhiteSpace, IsLetterOrDigit, IsPunctuation
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Program {
static void Main(string[] args) { Console.WriteLine("-> char.IsDigit("K"): {0}", char.IsDigit("K")); Console.WriteLine("-> char.IsDigit("9"): {0}", char.IsDigit("9")); Console.WriteLine("-> char.IsLetter("10", 1): {0}", char.IsLetter("10", 1)); Console.WriteLine("-> char.IsLetter("p"): {0}", char.IsLetter("p")); Console.WriteLine("-> char.IsWhiteSpace("Hello There", 5): {0}",char.IsWhiteSpace("Hello There", 5)); Console.WriteLine("-> char.IsWhiteSpace("Hello There", 6): {0}",char.IsWhiteSpace("Hello There", 6)); Console.WriteLine("-> char.IsLetterOrDigit("?"): {0}",char.IsLetterOrDigit("?")); Console.WriteLine("-> char.IsPunctuation("!"): {0}",char.IsPunctuation("!")); Console.WriteLine("-> char.IsPunctuation(">"): {0}",char.IsPunctuation(">")); Console.WriteLine("-> char.IsPunctuation(","): {0}",char.IsPunctuation(",")); }
}
</source>
Using Char
<source lang="csharp"> /*
* C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */
using System; namespace Client.Chapter_1___Common_Type_System {
public class UsingCharChapter_1___Common_Type_System { static void Main(string[] args) { char MyChar = "A"; MyChar = (char)65; char[] MyChar2 = { "H", "e", "l", "l", "o", "\0" }; char[] MyChar3 = new char[5]; MyChar3[0] = "H"; MyChar3[1] = "e"; MyChar3[2] = "l"; MyChar3[3] = "l"; MyChar3[4] = "o"; MyChar3[5] = "\0"; } }
}
</source>