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(',')); } }
Author: coder
A queue class for characters
/*
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("
");
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);
}
}
}
[/csharp]
A set class for characters
/*
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
Demonstrate the ICharQ interface: A character queue interface
/*
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("
Store 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);
}
}
}
[/csharp]
Demonstrate several Char methods
/*
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); } } [/csharp]
Encode or decode a message
/*
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;
}
}
[/csharp]
A stack class for characters
/*
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("
");
}
}
[/csharp]