12th Class (HSSC-II) Computer SLO Based Key Point Notes
👉👉👉 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.
- 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();
}
#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();
}
#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();
}
#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();
}
#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):
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();
}
👇👇👇👇👇👇👇👇👇👇
#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)
************************************
(National Book Foundation - As Federal Textbook Board, Islamabad Based on National Curriculum Pakistan 2026-2027 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards)
(National Book Foundation - As Federal Textbook Board, Islamabad
Based on National Curriculum Pakistan 2026-2027 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards)
{Contact WhatsApp # +923339719149 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}
Note: Write me in the comments box below for any query and also Share this information with your class-fellows and friends.
Note: Write me in the comments box below for any query and also Share this information with your class-fellows and friends.
0 Comments
Note: Write me in the comments box below for any queries and also Share this information with your class-fellows and friends.