0% found this document useful (0 votes)
4 views11 pages

C++ String Basics and Functions Guide

Uploaded by

9d.tanvishpatil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

C++ String Basics and Functions Guide

Uploaded by

9d.tanvishpatil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Strings

C++ - Programming
STRINGS
🠶 String is character array.
🠶 Many characters stored in contiguous memory locations.
🠶 Eg:
char name[20]= “RJ COLLEGE” ;

R J C O L L E G E \0

This is called NULL character &


marks the end of the string
Initialize a string
char nm[20]= “MANJIRI” ;
M A N J I R I \0

cout<<nm[0];
cout<<nm[1];
cout<<nm[2];
cout<< nm
cout<<nm[3];
cout<<nm[4];
cout<<nm[5];
Let’s Try:

🠶 Accept Name from user and print a welcome


message along with user’s name on screen.

🠶 Accept a word from user, count & print no. of


“a”s in it.

🠶 Accept a word and print the ascii value of each


character.
Commonly used String Functions….
#include<string.h>
Finds the no. of characters in the
strlen String

strcpy Copies one string onto the other

strrev Reverses the string

strcmp Compares two strings


Strlen( )
char nm[20];
int len;
printf(“Enter your name”); Do NOT forget to write
#include<string.h>
cin>>nm; In the beginning of the
program.

len = strlen(nm);

cout<<“length of string=”<<len;
strcpy( )
char x[20], y[20];

cout<<“Enter your name”;


cin>>x;

strcpy ( y , x );

cout<<“string 1 =”<< x ;
cout<<“string 2 =”<< y ;
strrev( )
char name[20], name_copy[20];
cout<<“Enter your name”;
cin>>name ;

strcpy(name_copy, strrev(name));

cout<<name_copy;
strcmp ( )
char S1[ ] =“ABC”;
A B C \0
char S2[ ] =“ABC”;
A B C \0

if ( strcmp(S1,S2)==0)
S1[0] – s2[0] =?
cout<<“SAME”;
else ‘A’ – ‘A’ =?

cout<<“DIFFERENT”;
65 - 65 = 0
Fill in the blanks:
(a)"A" is a ___________ where as ’A’ is a ____________.

(b) A string is terminated by a ______ character, which is


written as ______.

(c) The array char name[ 10 ] can consist of a maximum of


______ characters.

(d) The array elements are always stored in _________


memory locations.
Try yourself..

Accept User name and password from user. If the


User name is “RJC” and password is “student” ,
then print “LOGIN SUCCESSFUL” else print “LOGIN
FAILED”

You might also like