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
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