Coders Packet

It works only for Comparing and Swapping of string build using simple C++ functions

By Rupam Jha

  • __MACOSX/
  • COMPARING AND SWAPPING STRINGS IN C++.docx
  • We know that the string supports functions for comparing and swapping strings. In this tutorial we, will learn how to compare and swap strings in c++

     

    In this tutorial, we will learn how to compare and swap strings in C++.

    We know that the string supports functions for comparing and swapping strings.

    The compare() function is used to compare either two strings or portions of two strings.

    The swap() function is used to swap the contents of two string objects.

    Question:

    Given two strings s1&s2 compare and swap it.

     

    Here we can see the comparison & swapping of two strings.

    For using string class, we must include in our program.

    The statement:

    int x=s1.compare(s2);

    Compares the string s1 against s2 and x is assigned 0 if the strings are equal ,a positive number if s1 is lexicographically(means in dictionary order) greater than s2 or a negative number otherwise.

    Further,

    Another statement:

    s1.swap(s2);

    exchanges contents of strings s1 & s2. 

    Download Complete Code

    Comments

    No comments yet