0% found this document useful (0 votes)
1 views

Strings in C++

Strings C++ HSC 12th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Strings in C++

Strings C++ HSC 12th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Strings in C++

Wednesday, September 28,


MITHIBAI COLLEGE 1
2022
Strings in C++ ( Introduction)
• String is group of characters.
• No primitive(Built-in) data type for Strings in C++.
• But can be stored using Character Array.
• Strings in C++ are always terminated by special character
NULL (‘\0’) .

Wednesday, September 28,


KAUSHIK PANCHAL 2
2022
Strings

char str[10];

str ‘m’ ‘i’ ‘t’ ‘h’ ‘i’ ‘b’ ‘a’ ‘i’ X X


0 1 2 3 4 5 6 7 8 9

Wednesday, September 28,


KAUSHIK PANCHAL 3
2022
Strings

char str[10];

str ‘m’ ‘i’ ‘t’ ‘h’ ‘i’ ‘b’ ‘a’ ‘i’ ‘\0’ x

0 1 2 3 4 5 6 7 8 9

Null

Wednesday, September 28,


KAUSHIK PANCHAL 4
2022
Strings
• To read a string (input from user)
– cin >> str ;
– This will read only single word.
• To read a string (including spaces)
– cin.getline(str, 80);

Name of character Max. number of


array character to be read

Wednesday, September 28,


KAUSHIK PANCHAL 5
2022
Strings
• To display a string
– cout << str;

Name of character
array

Wednesday, September 28,


KAUSHIK PANCHAL 6
2022
Example
To read a line of text and display it. Also find the length of string.
#include <iostream.h> int len;
#include <conio.h>
void main() for(len=0; str[len]!=‘\0’;len++)
{ {}
char str[80]; cout<<“Length of string : “ << len;
clrscr();
cout<<"Enter a string : "; getch();
cin.getline(str,80); }
cout<<“String entered is : “;
cout << str ;

Wednesday, September 28,


KAUSHIK PANCHAL 7
2022
Programs for Practice
1. WAP to read a line of text and count number of words in it.
2. WAP to read a line of text and count occurrence of letter ‘J’ in
string.
3. WAP to read a line of text and replace spaces with hyphen
and display modified string.
4. WAP to read a line of text and copy it to another string
without using library function.
5. WAP to read a line of text and print each character in new line
in reverse order
Wednesday, September 28,
KAUSHIK PANCHAL 8
2022
WAP to read a line of text and count number of words in it.

#include <iostream.h> for(i=0; str[i]!=‘\0’;i++)


#include <conio.h> {
void main() if(str[i] == ‘ ‘)
{ word_count++;
char str[80]; }
int word_count = 0,i; word_count++; // no of spaces + 1
cout<<“Number of words : “
clrscr(); << word_count ;
cout<<"Enter a string : ";
cin.getline(str,80); getch();
cout<<“String entered is : “; }
cout << str ;

Wednesday, September 28,


KAUSHIK PANCHAL 9
2022
WAP to read a line of text and count occurrence of letter ‘J’ in string.
#include <iostream.h> for(i=0; str[i]!=‘\0’;i++)
#include <conio.h> {
void main() if(str[i] == ‘J‘)
{ count++;
char str[80]; }
int count = 0,i;
cout<<“ Count of letter J : “
clrscr(); << count ;
cout<<"Enter a string : ";
cin.getline(str,80); getch();
cout<<“String entered is : “; }
cout << str << endl;

Wednesday, September 28,


KAUSHIK PANCHAL 10
2022
WAP to read a line of text and replace spaces with hyphen and display modified string
#include <iostream.h> for(i=0; str[i]!=‘\0’;i++)
#include <conio.h> {
void main() if(str[i] == ‘ ‘)
{ str[i] = ‘-‘;
char str[80]; }
int i; cout<<“ Modified String : “ << str ;

clrscr(); getch();
cout<<"Enter a string : "; }
cin.getline(str,80);
cout<<“Original string : “;
cout << str << endl;

Wednesday, September 28,


KAUSHIK PANCHAL 11
2022
WAP to read a line of text and copy it to another string without using library function
#include <iostream.h> for(i=0 ; a[i]!=‘\0’ ; i++ )
#include <conio.h> {
void main() b[i] = a[i];
{ }
char a[80], b[80];
int i; b[i] = ‘\0’; // append null

clrscr(); cout<<“ Copied String : “ << b ;


cout<<"Enter a string : ";
cin.getline(a,80); getch();
cout<<“Original string : “; }
cout << a << endl;

Wednesday, September 28,


KAUSHIK PANCHAL 12
2022
WAP to read a line of text and print each character in new line in reverse order.
#include <iostream.h> // first find length of string
#include <conio.h> for(len=0 ; a[len]!=‘\0’ ; len++ )
void main() {}
{ cout<<“Reverse string : “;
char a[80];
int i,len; // then start printing from len - 1
for(i=len-1 ; i>=0 ; i-- )
clrscr(); {
cout<<"Enter a string : "; cout << a[i] << endl ;
cin.getline(a,80); }

cout<<“String entered is : “; getch();


cout<< a ; }

Wednesday, September 28,


KAUSHIK PANCHAL 13
2022
Programs
1. WAP to read a line of text and reverse it.
(user defined function)

Wednesday, September 28,


KAUSHIK PANCHAL 14
2022
Reverse string ( user defined function)
#include <iostream.h> void reverse ( char str[] )
#include <conio.h> {
void reverse ( char str[] ); int i,j,len;
void main()
{ for(len=0;str[len]!=‘\0’;len++)
char a[80]; {}
clrscr();
cout<<"Enter a string : "; for(i=0,j=len-1 ; i < j ; i++,j-- )
cin.getline(a,80); {
cout<<“String entered : “; char t = str[i];
cout<< a << endl; str[i] = str[j];
reverse(a); str[j] = t ;
cout<<“Reversed string : “ << a ; }
getch(); }
}

Wednesday, September 28,


KAUSHIK PANCHAL 15
2022
Strings in C++ ( library functions)
• Header file : string.h
• Example :
– strlen(a) : returns length of string stored in char array a.
– strcpy(s1,s2) : Copies string s2 to s1.
– strrev(s1) : reverses all the character in s1 (except null).
– strcmp(s1,s2) : Compares the string s1 with s2 and returns an integer
value.
• If s1 < s2 then returns value <0
• If s1 = s2 then returns value =0
• If s1 > s2 then returns value >0

Wednesday, September 28,


KAUSHIK PANCHAL 16
2022

You might also like