5.4 Commonly used String Function, Chapter - 5, Arrays and String, HSSC-II (12th Class) Computer Science Key Point Notes

5.4 Commonly used String Function, Chapter - 5, Arrays and String, HSSC-II (12th Class) Computer Science Key Point Notes

12th Class (HSSC-II) Computer SLO Based Key Point Notes

(National Book Foundation - As Federal Textbook Board, Islamabad 
Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards) 

{Contact WhatsApp 03339719149 for Class-XII Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation for preparing/delivering the Lectures}

************************************

👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************

Unit 5: Arrays and String

5.4 Commonly used String Function

1. cin.get() Function:

  • This function is used to read a string from the keyboard that may contain blank spaces.
  • The cin statement can also be used to read a string but it has the limitation that it considers a blank space as a terminating character. It can only read a string consisting of a single word. Anything typed after a space is ignored.

The syntax of cin.get():

                                    cin.get( str_var, str_size );

It has two arguments. Str_var is the name of the string and str_size is the maximum size of the string or array.

EXAMPLE

#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
        clarscr();
        char name[30];
        cout<<“Enter your Name: “;
        cin.get ( name, 30);
        cout<<“You Typed: “<<name;
        getch();
        }


2. strcpy() Function

This function is used to copy the contents of one string variable to another string variable.

Syntax:

            strcpy( string1 , string2 );


EXAMPLE

#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
        clarscr();
        char str1[10] = “PAKISTAN”, str2[10];
        strcpy( str2, str1 );
        cout<<str2;
        strcpy ( str2, “ZINDABAD”);
        cout<<str2;
        getch();
}


3. strcat() Function

It is used for concatenation or joining two strings. 

The general syntax is:

                                    strcat( str1, str2 );

EXAMPLE

#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
        clarscr();
        char str1[10] = “Home”, str2[10] = “Work”;
        strcat(str1, str2);
        cout<<str1;
        getch();
}


4. strlen() Function

• This function is used to return the length (the number of characters) of a string. 

The general syntax is:

                    strlen(string variable);

EXAMPLE

#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
        clarscr();
        char name[30] = “Zahid Hussain”;
        cout<<the number of characters are: “<<strlen(name);
        getch();
}


5. strcmp() Function

strcmp(string1, string2):

This function is used to compare two strings and return an integer value based on the comparison. i.e.




EXAMPLE

#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
        clarscr();
        char str1[10] = “mango”, str2[10]= “potato”, str3[10] = “mango”;
        int x;
        x= strcmp( str1, str2);
        cout<<x<<endl;
        x= strcmp( str3, str1);
        cout<<x<<endl;
        getch();
}


************************************
👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************


************************************
Shortcut Links For:

1.  5th Class All Subjects Notes

2.  8th Class All Subjects Notes

3.  Easy English Grammar Notes



1. Website for School and College Level Physics   
2. Website for School and College Level Mathematics  
3. Website for Single National Curriculum Pakistan - All Subjects Notes 

© 2023 & onwards Academic Skills and Knowledge (ASK  

Note:  Write me in the comments box below for any query and also Share this information with your class-fellows and friends.

Post a Comment

0 Comments

cwebp -q 80 image.png -o image.webp