+92 332 4229 857 99ProjectIdeas@Gmail.com

How to check the last character of the string (C++ Tutorial)


Ends with
EndsWith() compares input character with the character on the last index of the string. It returns true if the input character is equal to the character on the last index of the string and returns false if both are not equal.
This function takes 3 input parameters:
1.  str accepts character array(string).
2.  ch accepts the character to compare.
3.  ignoreCase accepts true or false, true if you want to ignore the case of string’s last index value and the character to compare and false in case if you don’t want to ignore both of them.


Code of EndsWith()
bool EndsWith( char * str , char ch , bool ignoreCase )
{
      int last = strlen( str ) - 1;

      if ( ignoreCase == false )
      {
            if ( str[last] == ch )
                  return true;
      }
      else if ( ignoreCase == true )
      {
            if ( str[last] == ch )
                  return true;
            else if ( IsAlphabatCapital( str[last] ) & IsAlphabatSmall( ch ) )
            {
                  if ( int( str[last] + 32 ) == ch )
                        return true;
            }
            else if ( IsAlphabatSmall( str[last] ) & IsAlphabatCapital( ch ) )
            {
                  if ( int( str[last] - 32 ) == ch )
                        return true;
            }
      }
      return false;
}

Summary of EndsWith()
If the value of ignoreCase is false
Then
If the value of the string’s last index and the character are equal
Then
return true
Else if the value of the ignoreCase is true
Then
If the value of the string’s last index and the character are equal
Then
return true
If the value of the string’s last index is capital and the character is small
Then
If the value of the string’s last index – converted to small and the character are equal
Then
return true
If the value of the string’s last index is small and the character is capital
Then
If the value of the string’s last index – converted to capital and the character are equal
Then
return true
      Returns false from the function.

Summary of IsAlphabatSmall()
IsAlphabatSmall() takes character as its input parameter and returns true if the character(alphabat) is small and returns false in either case.
Example
cout << IsAlphabatSmall(‘a’);
It will return true.
cout << IsAlphabatSmall(‘A’);
It will return false.

Summary of IsAlphabatCapital()
IsAlphabatCapital() takes character as its input parameter and returns true if the character(alphabat) is capital and returns false in either case.
Example
cout << IsAlphabatSmall(‘a’);
It will return false.
cout << IsAlphabatSmall(‘A’);
It will return true.

Example (C++)
#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

bool IsAlphabatSmall( char ch )
{
      if ( ch >= 97 & ch <= 122 )
            return true;
      return false;
}

bool IsAlphabatCapital( char ch)
{
      if ( ch >= 65 & ch <= 90 )
            return true;
      return false;
}

bool EndsWith( char * str , char ch , bool ignoreCase )
{
      int last = strlen( str ) - 1;

      if ( ignoreCase == false )
      {
            if ( str[last] == ch )
                  return true;
      }
      else if ( ignoreCase == true )
      {
            if ( str[last] == ch )
                  return true;
            else if ( IsAlphabatCapital( str[last] ) & IsAlphabatSmall( ch ) )
            {
                  if ( int( str[last] + 32 ) == ch )
                        return true;
            }
            else if ( IsAlphabatSmall( str[last] ) & IsAlphabatCapital( ch ) )
            {
                  if ( int( str[last] - 32 ) == ch )
                        return true;
            }
      }
      return false;
}

int main()
{
      cout << EndsWith( "saad" , 'd' , true ) << endl;      //    1
      cout << EndsWith( "SaaD" , 'd' , true ) << endl;      //    1
      cout << EndsWith( "saad" , 'D' , true ) << endl;      //    1

      cout << endl;

      cout << EndsWith( "saad" , 'd' , false ) << endl;     //    1
      cout << EndsWith( "SaaD" , 'd' , false ) << endl;     //    0
      cout << EndsWith( "saad" , 'D' , false ) << endl;     //    0

      cout << endl;

      cout << EndsWith( "saad" , 'A' , true ) << endl;      //    0
      cout << EndsWith( "SaaD" , 'B' , true ) << endl;      //    0
      cout << EndsWith( "saad" , 'C' , true ) << endl;      //    0
     
      cout << endl;

      cout << EndsWith( "saad" , 'a' , true ) << endl;      //    0
      cout << EndsWith( "SaaD" , 'b' , true ) << endl;      //    0
      cout << EndsWith( "saad" , 'c' , true ) << endl;      //    0
     
      _getche();
      return 0;
}

0 comments: