Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

C++

Strings -1
Lecture- 17

Raghav Garg
What are strings "O
and Why are
they used?
strings are basically car arrays.

Char str)]=I'a'b','d'e',"0'3;
I
raghar garg
Declaration of Strings
and taking Input
strings; Whav"
S
(ins;

conts;
Indexing of W
characters in v
Strings ~
strings:"Raghar" stitatio
sC0] - 'R'
Ques : Input a string of length n and count all
the vowels in the given string.
int n;

cinn;
char strin];
for (int 0;
=

i<n; i+ +)

I ain str[i];
3
Updation of a
single character
in string
012345

String str: "Ragbar";


strll] = '0';

cont<<str;

In C++, strings are metable


Ques : Input a string of size n and Update all
the even positions in the string to character
‘a’. Consider 0-based indexing.
A

Built-in string functions


reverse

length

substring
inte
string
etc

aplusplus. can
<string_name>.size()
push_back() Vectors

&
pop-back (S
“+” operator
reverse()
string str: "abcdef";
reverse (str. begin(), str.end());
Ques : Input a string of even length and
reverse the first half of the string.
01234567

string s -"Abdefgh";length - n 8
=

I/ reverse first half e0ton/2 se


debaefgh
coutS;
Ques : Input a string of length greater than 5 and
reverse the substring from position 2 to 5 using
inbuilt functions.
string str: "Raghar"
012345

reverse (str. begin)) 1, str. begin()


+

S);
+
substr() "abc";
d
string =

substring? ↓
a, ab, abc, b, bc, c
Ques : Input a string of even length and return the
second half of that string using inbuilt substr
function
to_string() Important
1
integer

It converts integer to
string

int n 12345
=
Ques : Return the total number of digits in a
number without using any loop.

Hint : Try using inbuilt to_string() function.


n 1234
=

4
Summary:
1) strings "abod";push-back(), pop-backl
=

2) functions builtin - length, to-string, substr, reverse,

cincstr, (cin, str)


3)
L
getline,
entire line
one

word

4) S: "abc", t "xyz"
=

S S +
= +
+ 5 abcxyz
=
Thank you!

You might also like