Wednesday, 4 April 2018

Pointers to Strings

A string is an array of characters terminated by a special character called as a null character.

Pointer to the string is a pointer which is initialized to the base address of the first location in string.

Syntax for declaring a pointer to string.

Datatype *pointer_variable;
Ex : char *ptr;

To initialize the pointer to the address of the string , assign the pointer to the name of string or to the address of the first element of the string.

char str[10]="Irawen";

char *ptr;

ptr=str;    OR       ptr=&str[0];

WAP to find the length of string using pointer

#include<conio.h>
#include<iostream.h>
void main( )
{
char str[10], *ptr;
int cnt=0;
clrscr( );
cout<<"\n\n\tEnter Any String";
cin>>str;
ptr=&str[0];
while(*ptr!='\0')
{
cnt++;
ptr++;
}
cout<<"\n\n\tLength of the Given String is--->"<<cnt;
getch( );
}

Output:-
Enter Any String Irawen
Length of the Given String is--->6

WAP to copy one string into another string and print both the strings on output screen using pointers to strings.

#include<conio.h>
#include<iostream;h>
void main( )
{
char str1[20], str2[20];
char *ptr1, *ptr2;
clrscr( );
cout<<"\n\n\tEnter First String";
cin>>str1;
ptr1=&str1[0];
ptr2=&str2[0];
while(*ptr1!='\0')
{
*ptr2=*ptr1;
ptr1++;
ptr2++;
}
*ptr2='\0';
cout<<"\n\n\tEnter String is--->"<<str1;
cout<<"\n\n\tCopied String is--->"<<str2;
getch( );
}

Output:-
Enter First String Irawen
First String is--->Irawen
Coiped String is--->Irawen

WAP to concat two strings by using pointers to strings.

#include<conio.h>
#include<iostream.h>
void main( )
{
char str1[20], str2[10], *ptr1, *ptr2;
clrscr( );
cout<<"\n\n\tEnter the First String";
cin>>str1;
cout<<"\n\n\tEnter the Second String";
cin>>str2;
ptr1=&str1[0];
ptr2=&str2[0];
while(*ptr1!='\0')
{
ptr1++;
}'
while(*ptr2!='\0')
{
*ptr1=*ptr2;
ptr1++;
ptr2++;
}
*ptr1='\0'
cout<<"\n\n\tConcatenated String is--->"<<str1;
getch( );
}

Output:-
Enter the First String Ira
Enter the Second String wen
Concatenated String is--->Irawen

WAP to reverse the given string using pointers

#include<conio.h>
#include<iostream.h>
void main( )
{
char str1[10], *ptr1;
int len=0;
clrscr( );
cout<<"\n\n\tEnter the String";
cin>>str1;
ptr1=&str1[0];
while(*ptr1!='\0')
{
len=len+1;
ptr1++;
}
len--;
ptr1--;
cout<<"\n\n\tReverse String is--->";
while(len>0)
{
cout<<*ptr1;
ptr1--;
len--;
}
getch( );
}

Output:-
Enter the String irawen
Reverse String is--->newari

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses