+92 332 4229 857 99ProjectIdeas@Gmail.com

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


Starts with
StartsWith() compares input character with the character on the first index of the string. It returns true if the input character is equal to the character on the first 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 first index value and the character to compare and false in case if you don’t want to ignore both of them.


Code of StartsWith()
bool StartsWith( char * str , char ch , bool ignoreCase )
{
      if ( ignoreCase == false )
      {
            if ( str[0] == ch )
                  return true;
      }
      else if ( ignoreCase == true )
      {
            if ( str[0] == ch )
                  return true;
            else if ( IsAlphabatCapital( str[0] ) & IsAlphabatSmall( ch ) )
            {
                  if ( int( str[0] + 32 ) == ch )
                        return true;
            }
            else if ( IsAlphabatSmall( str[0] ) & IsAlphabatCapital( ch ) )
            {
                  if ( int( str[0] - 32 ) == ch )
                        return true;
            }
      }
      return false;
}

Summary of StartsWith()
If the value of ignoreCase is false
Then
If the value of the string’s first 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 first index and the character are equal
Then
return true
If the value of the string’s first index is capital and the character is small
Then
If the value of the string’s first index – converted to small and the character are equal
Then
return true
If the value of the string’s first index is small and the character is capital
Then
If the value of the string’s first 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 StartsWith( char * str , char ch , bool ignoreCase )
{
      if ( ignoreCase == false )
      {
            if ( str[0] == ch )
                  return true;
      }
      else if ( ignoreCase == true )
      {
            if ( str[0] == ch )
                  return true;
            else if ( IsAlphabatCapital( str[0] ) & IsAlphabatSmall( ch ) )
            {
                  if ( int( str[0] + 32 ) == ch )
                        return true;
            }
            else if ( IsAlphabatSmall( str[0] ) & IsAlphabatCapital( ch ) )
            {
                  if ( int( str[0] - 32 ) == ch )
                        return true;
            }
      }
      return false;
}

int main()
{
      cout << StartsWith( "saad" , 's' , true ) << endl;    //    1
      cout << StartsWith( "Saad" , 's' , true ) << endl;    //    1
      cout << StartsWith( "saad" , 'S' , true ) << endl;    //    1

      cout << endl;

      cout << StartsWith( "saad" , 's' , false ) << endl;   //    1
      cout << StartsWith( "Saad" , 's' , false ) << endl;   //    0
      cout << StartsWith( "saad" , 'S' , false ) << endl;   //    0
     
      cout << endl;

      cout << StartsWith( "Saad" , 'A' , true ) << endl;    //    0
      cout << StartsWith( "Saad" , 'B' , true ) << endl;    //    0
      cout << StartsWith( "Saad" , 'C' , true ) << endl;    //    0
     
      cout << endl;

      cout << StartsWith( "Saad" , 'a' , true ) << endl;    //    0
      cout << StartsWith( "Saad" , 'b' , true ) << endl;    //    0
      cout << StartsWith( "Saad" , 'c' , true ) << endl;    //    0
     
      _getche();
      return 0;
}

0 comments: