C++/Data Type/Char
Содержание
- 1 cin and cout work with char array
- 2 Convert a number to a char then convert to upper case and lower case
- 3 Declare a stack class for characters.
- 4 Declares str just before it is needed
- 5 Enters a character and outputs its octal, decimal, and hexadecimal code.
- 6 Overload string reversal function.
- 7 Read string and output its length
- 8 Using C strings: cin,=.sync, getline
- 9 Using functions isdigit, isalpha, isalnum, and isxdigit
- 10 Using functions islower, isupper, tolower, toupper
- 11 Using functions isspace, iscntrl, ispunct, isprint, isgraph
- 12 Wrap char pointer to a String class
cin and cout work with char array
#include <iostream>
using namespace std;
int main ()
{
char name[ 80] = {"J", "e", "f", "f", "/0" };
cout << "Enter your name: ";
cin >> name;
cout << "Your name is " << name;
return 0;
}
Convert a number to a char then convert to upper case and lower case
#include <iostream>
using namespace std;
int main()
{
char a,u,l;
int x;
cout << "Please enter a number \n";
cin >> a;
x = atoi(&a);
cout << "The character " << a << " is converted to the integer " << x << "\n";
cout << "Please enter a character \n";
cin >> a;
u = toupper(a);
l = tolower(a);
cout << a << " in upper case is " << u << " in lower case is " << l;
return 0;
}
Declare a stack class for characters.
#include <iostream>
using namespace std;
#define SIZE 10
class stack {
char stackData[SIZE]; // holds the stack
int topOfStack; // index of top-of-stack
public:
stack() { topOfStack = 0; }
void push(char ch)
{
if(topOfStack==SIZE) {
cout << "Stack is full\n";
return;
}
stackData[topOfStack] = ch;
topOfStack++;
}
char pop()
{
if(topOfStack==0) {
cout << "Stack is empty\n";
return 0; // return null on empty stack
}
topOfStack--;
return stackData[topOfStack];
}
};
int main()
{
stack stackObject1, stackObject2;
int i;
stackObject1.push("a");
stackObject2.push("x");
stackObject1.push("b");
stackObject2.push("y");
stackObject1.push("c");
stackObject2.push("z");
for(i = 0; i <3; i++)
cout << "Pop stackObject1: " << stackObject1.pop() << endl;
for(i = 0; i <3; i++)
cout << "Pop stackObject2: " << stackObject2.pop() << endl;
return 0;
}
Declares str just before it is needed
#include <iostream>
using namespace std;
int main()
{
float f;
double d;
cout << "Enter two floating point numbers: ";
cin >> f >> d;
cout << "Enter a string: ";
char str[80];
cin >> str;
cout << f << " " << d << " " << str;
return 0;
}
Enters a character and outputs its octal, decimal, and hexadecimal code.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int number = " ";
cout << "The white space code is:" << number << "." << endl;
char ch;
string prompt = "Please enter a character followed by <return>: ";
cout << prompt;
cin >> ch;
number = ch;
cout << "The character " << ch << " has code" << number << endl;
cout << uppercase // For hex-digits
<< " octal decimal hexadecimal\n "
<< oct << setw(8) << number
<< dec << setw(8) << number
<< hex << setw(8) << number << endl;
return 0;
}
Overload string reversal function.
#include <iostream>
#include <cstring>
using namespace std;
void reverseString(char *s); // reverse string in place
void reverseString(char *in, char *out); // put reversal into out
int main()
{
char s1[80], s2[80];
strcpy(s1, "This is a test");
reverseString(s1, s2);
cout << s2 << endl;
reverseString(s1);
cout << s1 << endl;
return 0;
}
// Reverse string, put result in s.
void reverseString(char *s)
{
char temp[80];
int i, j;
for(i=strlen(s)-1, j=0; i>=0; i--, j++)
temp[j] = s[ i ];
temp[j] = "\0"; // null terminate result
strcpy(s, temp);
}
// Reverse string, put result into out.
void reverseString(char *in, char *out)
{
int i, j;
for(i=strlen(in)-1, j=0; i>=0; i--, j++)
out[j] = in[ i ];
out[j] = "\0"; // null terminate result
}
Read string and output its length
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s[80];
cout << "Enter a string: ";
cin >> s;
cout << "Length: " << strlen(s) << endl;
return 0;
}
Using C strings: cin,=.sync, getline
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
char header[] = "C Strings\n";
int main()
{
char hello[30] = "Hello ", name[20], message[80];
cout << header << "Your first name: ";
cin >> setw(20) >> name;
strcat( hello, name);
cout << hello << endl;
cin.sync();
cout << "\nWhat is the message for today?"
<< endl;
cin.getline( message, 80);
if( strlen( message) > 0) {
for( int i=0; message[i] != "\0"; ++i)
cout << message[i] << " ";
cout << endl;
}
return 0;
}
Using functions isdigit, isalpha, isalnum, and isxdigit
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
cout << "According to isdigit:\n"
<< ( isdigit( "8" ) ? "8 is a" : "8 is not a" )
<< " digit\n"
<< ( isdigit( "#" ) ? "# is a" : "# is not a" )
<< " digit\n";
cout << "\nAccording to isalpha:\n"
<< ( isalpha( "A" ) ? "A is a" : "A is not a" )
<< " letter\n"
<< ( isalpha( "b" ) ? "b is a" : "b is not a" )
<< " letter\n"
<< ( isalpha( "&" ) ? "& is a" : "& is not a" )
<< " letter\n"
<< ( isalpha( "4" ) ? "4 is a" : "4 is not a" )
<< " letter\n";
cout << "\nAccording to isalnum:\n"
<< ( isalnum( "A" ) ? "A is a" : "A is not a" )
<< " digit or a letter\n"
<< ( isalnum( "8" ) ? "8 is a" : "8 is not a" )
<< " digit or a letter\n"
<< ( isalnum( "#" ) ? "# is a" : "# is not a" )
<< " digit or a letter\n";
cout << "\nAccording to isxdigit:\n"
<< ( isxdigit( "F" ) ? "F is a" : "F is not a" )
<< " hexadecimal digit\n"
<< ( isxdigit( "J" ) ? "J is a" : "J is not a" )
<< " hexadecimal digit\n"
<< ( isxdigit( "7" ) ? "7 is a" : "7 is not a" )
<< " hexadecimal digit\n"
<< ( isxdigit( "$" ) ? "$ is a" : "$ is not a" )
<< " hexadecimal digit\n"
<< ( isxdigit( "f" ) ? "f is a" : "f is not a" )
<< " hexadecimal digit" << endl;
return 0;
}
Using functions islower, isupper, tolower, toupper
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
cout << "According to islower:\n"
<< ( islower( "p" ) ? "p is a" : "p is not a" )
<< " lowercase letter\n"
<< ( islower( "P" ) ? "P is a" : "P is not a" )
<< " lowercase letter\n"
<< ( islower( "5" ) ? "5 is a" : "5 is not a" )
<< " lowercase letter\n"
<< ( islower( "!" ) ? "! is a" : "! is not a" )
<< " lowercase letter\n";
cout << "\nAccording to isupper:\n"
<< ( isupper( "D" ) ? "D is an" : "D is not an" )
<< " uppercase letter\n"
<< ( isupper( "d" ) ? "d is an" : "d is not an" )
<< " uppercase letter\n"
<< ( isupper( "8" ) ? "8 is an" : "8 is not an" )
<< " uppercase letter\n"
<< ( isupper("$") ? "$ is an" : "$ is not an" )
<< " uppercase letter\n";
cout << "\nu converted to uppercase is "
<< ( char ) toupper( "u" )
<< "\n7 converted to uppercase is "
<< ( char ) toupper( "7" )
<< "\n$ converted to uppercase is "
<< ( char ) toupper( "$" )
<< "\nL converted to lowercase is "
<< ( char ) tolower( "L" ) << endl;
return 0;
}
Using functions isspace, iscntrl, ispunct, isprint, isgraph
#include <iostream>
#include <ctype.h>
using namespace std;
int main(){
cout << "According to isspace:\nNewline "
<< ( isspace( "\n" ) ? "is a" : "is not a" )
<< " whitespace character\nHorizontal tab "
<< ( isspace( "\t" ) ? "is a" : "is not a" )
<< " whitespace character\n"
<< ( isspace( "%" ) ? "% is a" : "% is not a" )
<< " whitespace character\n";
cout << "\nAccording to iscntrl:\nNewline "
<< ( iscntrl( "\n" ) ? "is a" : "is not a" )
<< " control character\n"
<< ( iscntrl( "$" ) ? "$ is a" : "$ is not a" )
<< " control character\n";
cout << "\nAccording to ispunct:\n"
<< ( ispunct( ";" ) ? "; is a" : "; is not a" )
<< " punctuation character\n"
<< ( ispunct( "Y" ) ? "Y is a" : "Y is not a" )
<< " punctuation character\n"
<< ( ispunct("#") ? "# is a" : "# is not a" )
<< " punctuation character\n";
cout << "\nAccording to isprint:\n"
<< ( isprint( "$" ) ? "$ is a" : "$ is not a" )
<< " printing character\nAlert "
<< ( isprint( "\a" ) ? "is a" : "is not a" )
<< " printing character\n";
cout << "\nAccording to isgraph:\n"
<< ( isgraph( "Q" ) ? "Q is a" : "Q is not a" )
<< " printing character other than a space\nSpace "
<< ( isgraph(" ") ? "is a" : "is not a" )
<< " printing character other than a space" << endl;
return 0;
}
Wrap char pointer to a String class
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class StringClass {
char *p;
int len;
public:
StringClass();
StringClass(char *s, int l);
char *getstring() {
return p;
}
int getlength() {
return len;
}
};
StringClass::StringClass()
{
p = new char [255];
if(!p) {
cout << "Allocation error\n";
exit(1);
}
*p = "\0"; // null string
len = 255;
}
StringClass::StringClass(char *s, int l)
{
if(strlen(s) >= l) {
cout << "Allocating too little memory!\n";
exit(1);
}
p = new char [l];
if(!p) {
cout << "Allocation error\n";
exit(1);
}
strcpy(p, s);
len = l;
}
int main()
{
StringClass stringObject1;
StringClass stringObject2("www.java2s.com", 100);
cout << "stringObject1: " << stringObject1.getstring() << " - Length: ";
cout << stringObject1.getlength() << "\n";
cout << "stringObject2: " << stringObject2.getstring() << " - Length: ";
cout << stringObject2.getlength() << "\n";
return 0;
}