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

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (726) Python Coding Challenge (169) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

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