Monday, June 4, 2012

Switch H3C S3100 CLI Configuration

Resetting a Switch to Default Configuration:
A switch saves all configuration to a file.
In order to reset it to default configuration we have to delete the configuration file in user view
with the command:
reset saved-configuration main


Enable Power over Ethernet (PoE) on a Port
  1. We have to be in system view to specify a port. To do this, type the command: system-view
  2. Command to enter Ethernet port view: interface ethernet interfaceNumber example: interface ethernet 1/0/2
  3. Enable the PoE feature on a port with the command: poe enable
Display PoE Configuration
Use the command: display poe interface

Tuesday, January 31, 2012

Ignoring Case when Comparing Strings

strcmpi() is the right function to compare strings while ignoring their cases (upper case or lower case).

Instead of looping an entire string just to convert each character to upper or lower case using toupper() or tolower(), we simply use strcmpi().


Sample Program:
Output:

Note: strcmpi() accepts 2 parameters, both are of type char[] that is why we called the function c_str() to convert s1 and s2 from string to char[].

Converting int to string


There are two ways to convert int to string.

The first way:
  1. use itoa()
  2. copy the resulting char[] to a new string by assignment ‘=’

     Sample Program:

    This is highly recommended when we need to convert base 10 to another base.
(decimal to hexadecimal or decimal to binary)

The second way:
  1. Create stringstream variable (preprocessor directive sstream is required)
  2. Use its str()
     Sample Program:

Output of both programs:

If your problem simply needs a conversion from int to string, use the second method.

Converting a String to Character Array

First of all, character arrays (char[]) are terminated by '\0'.
For illustration, visit  http://www.cplusplus.com/doc/tutorial/ntcs/ 

Why do we need this conversion?
Most of the commonly used functions accept char[] type instead of string as parameters.

  • strcpy()
  • strcmpi()
  • strtok()

So far, I have known two ways to convert a string to array of characters.
  1. copy()
  2. c_str()
I won't be discussing about copy() since this will require you to manually terminate the char[]. Meaning, you will have to determine where to put '\0' -usually right after the index of the last non-null element. To know more about this, you can visit
http://www.cplusplus.com/reference/string/string/copy/.

c_str() is easier to use because it automatically assigns '\0' after the last non-null element.


Syntax:
s.c_str() //s previously declared as string

Sample Program:
The program copies string s to char carr[].

strcpy() makes this type of copying possible, accepting two char[] as parameters.
The first is the destination char[] where the content is to be copied.
The second is the source, the char[] to be copied.

Since variable s is a string, we converted it to char[] by issuing s.c_str().

Friday, January 27, 2012

Comparing Strings

Comparing strings is vital in programming particularly in sorting.

For example, we want to arrange names in alphabetical order. Since names are stored as strings, we need to understand the string's compare() method.

Syntax:
string1.compare(string2);

Condition Return Value 
if string1 is equal to string2          0
if string1 is greater than string2             > 0
if string1 is less than string2         < 0


Sample Code:
#include <iostream>
#include <string>
using namespace std;
int main(){
     // First
     string string1 = "abc";
     string string2 = "bcd";
     string1.compare(string2);  // returns less than 0 because abc comes before bcd (a<b)
     // Second
     string1 = "HELLO";
     string2 = "ALOHA";
     string1.compare(string2); // returns greater than 0 because HELLO comes after ALOHA (H>A)
     //Third
     string1 = "Hello";
     string2 = "Hello";
     string1.compare(string2); // returns 0 because Hello and Hello are equal
     //Fourth
     string1 = "hello";
     string2 = "Hello";
     string1.compare(string2); // returns greater than 0 because hello comes after Hello
     return 0;
}

The fourth example returns greater than 0 because according to the ASCII table, h > H.

Small letters have greater decimal values than capital letters.
The decimal value of h is 104 while H is 72.


ASCII Table

Wednesday, January 18, 2012

cin string with white spaces

We use cin to store the user input to a specific variable.
Its limitation is when we try to save a string with white spaces (several words are involved).



The user input is "Hello there! Are you okay?", but only the first word, "Hello" is stored to variable str.


To include all words, we will use the function getline().





If you want to know more about getline(), visit the link below.

http://www.cplusplus.com/reference/string/getline/