We can access the string by using the pointer variable. Here is a simple C program that illustrates the use of pointer for string.
6. Strings and Pointers
We
can access the string by using the pointer variable. Here is a simple C program
that illustrates the use of pointer for string.
Program
for accessing the string using a pointer variable.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define
size 10
void
main(void)
{
char
name[size];
char
*i;
clrscr();
printf("\n
What is Your name?");
gets(name);
i=name;
printf("\nNow
printing your name as ");
while(*i!=
'\0')
{
printf("%c",
*i);
i++;
}
getch();
}
Output
What
is Your name? Parth
Now
printing your name as Parth
Now
we will use the pointer arithmetic and copy the contents of one string to
another using pointer variable.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void
main(void)
{
char
*ptr1; /* a pointer to type character */
char
*ptr2; /* another pointer to type character */
char
str1[] = "Hello India";
char
str2[20];
clrscr();
printf("\n
The first string i.e. str1 is: ");
puts(str1);
/* displays first string */
ptr1 = str1; /* pointer to str1 is set */
printf("\n The pointer ptr1 contains:
%s",ptr1);
ptr2 = str2; /* pointer to str2 is set */
while(*ptr1 != '\0')
{
/*copying the contents of ptr1 to ptr2*/
*ptr2++=
*ptr1++;
}
*ptr2
= '\0';/*placing the '\0' at the end of the string*/
printf("\n\n
The second string i.e. str2 is: ");
puts(str2);
/* displays the second string */
getch();/*to
avoid pressing alt+F5 to see the console*/
}
Output
The
first string i.e. str1 is: Hello India
The
pointer ptr1 contains: Hello India
The
second string i.e. str2 is: Hello India
In
above program, there are two strings str1
and str2. The string str1 contains a
string "Hello India"
We
have taken two pointer variables: ptr1
and ptr2. The starting address (base
address) is assigned to ptr1.
ptr1=str1;
Now
we can get the same string "Hello India" in ptr1. Now we have set the
ptr2 as a pointer to second string str2.
ptr2
= str2;
There
is nothing in str2, so we will copy the contents of first string to second
string using pointers
while(*ptr1
!= '\0')
{/*copying
the contents of ptr1 to ptr2*/
*ptr2++=
*ptr1++;
}
To
terminate str2 we will put '\0' at the end of str2 by assigning
*ptr2
= '\0';
Finally
we have printed the contents of str2.
Key Point:
We can store one string in a pointer variable of character type. i.e
char *ptr="Hello";
is
possible. We can apply pointer arithmetic to access the characters within the
string.
Ex.
2.5.8 Write a C program to concatenate two strings using pointer.
Sol.
:
#include<stdio.h>
#include
<conio.h>
void
main()
{
char
str1[20],str2[10];
char
*ptr1,*ptr2;
printf("\n
Enter first string ");}
gets(str1);
printf("\n
Enter second string ");
gets(str2);
ptr1=str1;
ptr2=str2;
while(*ptr1!=
'\0')//reaching at the end of first string
*ptr1++;
while(*ptr2!=
'\0')
{
*ptr1=
*ptr2;
*ptr2++;
*ptr1++;
}
*ptr1='\0';
printf("\n
The concatenated string is %s", str1);
getch();
}
Output
Enter
first string Hello
Enter
second string India
The
concatenated string is HelloIndia
Ex.
2.5.9 Write a "C" function using pointer for string reversal.
Sol. Program for string reversal -
void
main()
{
char
s1[10],s2[10];
clrscr();
printf("\n
Enter The String");
scanf("%s",s1);
char
*i=s1;
char
*j=s2;
while(*i!=
'\0')
i++;
i--;
/*reaching at the last character*/
while(i>=&s1[0])/*
copying the characters*/
*j++=*i--;
*j='\0';
printf("\n
The Reversed string is %s",s2);
getch();
}
Ex.
2.5.10 Write pseudo C routine using pointers for checking whether a given
string is palindrome.
Sol. Palindrome (char* a) // a is a
string
{
int
*p, *q; int flag = 0;
p
= q = a;
while
(* q!= '10')
q++;
q
--;
while
(p < q)
{
if
(* p! = *q)
flag
= 1;
p++;
q--;
}
if
(flag ==0)
printf
("string is palindrome");
else
printf
("\n string is not palindrome");
}
Ex.
2.5.11 Write a C program to implement any four string handling functions using
functions and pointers.AU
May-17, Marks 8
Sol. :
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int
main(void)
{
int
choice;
void
str_len(),str_concat(), str_copy(), str_reverse();
char
ans='y';
do
{
printf("\n
Main Menu");
printf("\n1.
Length \n2. Concatenation \n3. Copy \n4. Reverse");
printf("\n
Enter your choice: ");
scanf("%d",
&choice);
switch(choice)
{
case
1:str_len();
break;
case
2:str_concat();
break;
case
3:str_copy();
break;
case
4:str_reverse();
break;
}
printf("\n
Do you want to go to main menu?");
}
while(ans=='y' || ans == 'Y');
}
void
str_len()
{
char
*ptr,str[10];
int
len=0;
printf("Enter
some string: ");
scanf("%s",
str);
ptr=str;
while(*ptr!='\0')
{
*ptr++;
len++;
}
printf("\n
The length of string is: %d ",len);
}
void
str_concat()
{
char
str1[20], str2[10];
char
*ptr1,*ptr2;
printf("\n
Enter first string ");
scanf("%s",
str1);
printf("\n
Enter second string ");
scanf("%s",str2);
ptr1=str1;
ptr2=str2;
while(*ptr1!=
'\0')//reaching at the end of first string
*ptr1++;
while(*ptr2!='\0')
{
*ptr1=*ptr2;
*ptr2++;
*ptr1++;
}
*ptr1='\0';
printf("\n
The concatenated string is %s", str1);
}
void
str_copy()
{
char
*ptr1; /* a pointer to type character */
char
*ptr2; /* another pointer to type character */
char
str1[10],str2[20];
printf("\n
Enter the first string: ");
scanf("%s",
str1);
printf("\n
The first string i.e. str1 is: ");
puts(str1);
/* displays first string */
ptr1
= str1; /* pointer to str1 is set */
ptr2
= str2; /* pointer to str2 is set */
while(*ptr1
!= '\0')
{
/*copying the contents of ptr1 to ptr2*/
*ptr2++
= *ptr1++;
}
*ptr2
= '\0';/*placing the '\0' at the end of the string*/
printf("\n\n
The copied string is: ");
puts(str2);
/* displays the second string */
}.
void
str_reverse()
{
char
str1[10],str2[10];
printf("\n
Enter The String");
scanf("%s",
str1);
char
*ptr1=str1;
char
*ptr2=str2;
while(*ptr1!=
'\0')
ptr1++;
ptr1--;
while(ptr1>=&str1[0])/*copying
the characters*/
*ptr2++=
*ptr 1--;-
*ptr2='\0';
printf("\n
The Reversed string is %s", str2);
}
The
pointer to the function means a pointer variable that stores the address of
function. The function has an address in the memory same like variable. As
address of function name is a memory location we can have a pointer variable
which will hold the address of function. The data type of pointer will be same
as the return type of the function. For instance: if the return type of the
function is int then the integer pointer variable should store the address of
that function.
Syntax
Return_Type
*pointer_variable (data_type);
For example,
float
(*fptr)(float);
Here
fptr is a pointer to the function
which has float parameter and returns the value float. Note that the
parenthesis around fptr otherwise the meaning will be different. For example,
float
*fptr(float);
This
means fptr is a function with a
float parameter and returns a pointer to float.
float
fun(float);
float
(*fptr) (float);
fptr=&fun;
Thus
/*function
returning pointer to float */
float
*fptr (float a);
/*pointer
to function returning float */
float
(*fptr) (float a);
The
following program illustrates the use of pointer to the function
#include<stdio.h>
#include<conio.h>
void
main()
{
void
display(float(*)(int),int);
float
area(int);
int
r;
printf("\n
Enter the radius ");
scanf("%d",
&r);
display(area,r);/*function
is passed as a parameter to another function*/
}
void
display(float (*fptr) (int), int r)
{
/*call
to pointer to function*/
printf("\n
The area of circle is %f", (*fptr) (r));
}
float
area(int r)
{
return
(3.14*r*r);
}
Output
Enter
the radius 10
The
area of circle is 314.000000
Program Explanation
The
function display calls the function area through pointer variable fptr. Thus fptr is actually a pointer
to the function area. As function area returns the float value we have the
pointer as of float type.
Now
we will discuss "How to pass a pointer variable as a parameter to the function?"
Let us understand it with the help of following C program
Passing
a pointer variable to the function
#include
<stdio.h>
#include
<conio.h>
#include
<stdlib.h>
void
main()
{
int
*m;
void
fun(int *);
*m=5;
clrscr();
printf("\n
Following value is just before function call \n");
printf("%d",
*m);
printf("\n
Following value is obtained from the function \n");
fun(m);
printf("%d",*m);
}
void
fun(int *x)
{
*x=10;
}
Output
Following
value is just before function call
5
Following
value is obtained from the function
10
Ex.
2.5.12 What is the effect of the following statements :
i) int a, *b = Ɛa ii) int p, *p iii) a = (float *)Ɛ x iv) int (*fun) ().
Sol. :
i)
The pointer variable b is initialized by an address of a.
ii)
It will cause an error "multiple declaration for p"
iii)
Variable x may be of int type, hence to store the address of x into a float
type variable, the type casting to pointer type float value is done.
iv)
This is pointer to a function. This function returns the pointer to integer value.
Ex.
2.5.13 Write a "C" function using pointers to multiply two matrices
and return the resultant matrix to calling function.
Sol. : Multiplication
of two matrices using pointers
int
*mul(int m,int n,int (*a) [3], int (*b)[3], int (*c)[3])
{
int
i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
*(*(c+i)+j)=0;
for(k=0;k<m;k++)
*(*(c+i)+j)=
*(*(c+i)+j)+((*(*(a+i)+k))*(*(*(b+k)+j)));
}
}
return
*(c+0);
}
Review Questions
1. What is a pointer
variable? Explain declaration, initialization and accessing a pointer variable
with an example.
2. What is the data
type pointer in C? How do we declare the pointer? Give any four advantages of
using pointers.
3. Explain pointer
variable with example.
4. Explain about how
to declare pointer to a function with an example. AU: May-17, Marks 8
C Programming and Data Structures: Unit II: C Programming – Advanced Features : Tag: : Example C programs - Strings and Pointers
C Programming and Data Structures
CS3353 3rd Semester EEE, ECE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation