Strings and Pointers.pdf
Strings and Pointers.pdf
C language the group of characters, digits and symbols enclosed within quotation mark are
called as string. The string is always declared as character arrays. In other words character arrays
are called strings. To manipulate text such as words and sentences normally strings are used.
Every string is terminated with ‘\0’ (NULL) character. The NULL character is a byte with all
bits at logic zero. Hence, its decimal value is zero.
char a[10];
or
char string[] = {'A', 'N', 'A', 'N', 'T', 'A', 'P', 'U', 'R',’A’,’M’,’U’};
In the first example, the string will have a null character automatically appended to the
end by the compiler; by convention, library functions expect strings to be terminated by a null
character. The latter declaration indicates individual elements, and as such the null terminator
needs to be added manually.
Char st1[10]={“INDIA”}
I N D I A \0
p. Write a program to initialize the string and display that string
#include<stdio.h>
#include<conio.h>
void main()
{
char st1[20]={"INDIA"};
char st2[20]={'A','N','A','T','A','P','U','R'};
int i;
clrscr();
printf("\n initialized strings are \n");
i=0;
while(st1[i]!='\0')
{
1
printf("%c",st1[i]);
i++;
}
printf("\n");
i=0;
while(st2[i]!='\0')
{
printf("%c",st2[i]);
i++;
}
printf("\nst1=%s",st1);
printf("\nst2=%s",st2);
getch();
}
Output:
initialized strings are
INDIA
ANATAPUR
st1=INDIA
st2=ANATAPUR
The printf() function is used for display the various data types. The printf() function with
%s format is to be used for displaying the string on the screen. Various formats are shown in the
bellow program.
Write a program to display of string with different formats
#include<stdio.h>
#include<conio.h>
void main()
{
char st1[20]={"INDIA"};
char st2[20]={'A','N','A','T','A','P','U','R'};
int i;
clrscr();
printf("\nsrtings in different formats");
printf("\nst1=%.2s",st1);
printf("\nst2=%.4s",st2);
printf("\nst2=%.10s",st2);
getch();
}
2
Output :
srtings in different formats
st1=IN
st2=ANAT
st2=ANATAPUR
gets() : - the gets() function is used to read the string from keyboard at the time of running the
program.
Difference between scanf() and gets() functions
Both the functions used to read the strings at the time of running the program. But scanf()
stop the reading string when it find the space. In gets() function read the string until press the
enter key, including spaces also
Write a program to differentiate the scanf() and gets() functions?
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("\nread the string using gets : ");
gets(str1);
printf("\nread the string using scanf : ");
scanf("%s",str2);
printf("\nstring from gets str1=%s",str1);
printf("\nstring from scanf str2=%s",str2);
getch();
}
Output:
read the string using gets : this is c program
Write a program to find number of characters and number of words in the string.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[100];
int i,w,c;
clrscr();
3
printf("\nenter a string : ");
gets(str);
i=0;
w=1;
c=0;
while(str[i]!='\0')
{
if(str[i]==' ')
w++;
else
c++;
i++;
}
printf("\n no of words is : %d",w);
printf("\n no of characters is : %d",c);
getch();
}
Output:
enter a string : we are from anantapur
no of words is : 4
no of characters is : 18
Write a program to find reverse of the string and check the given string is palindrome or
not
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20],rev[20];
int i,j;
clrscr();
printf("\nenter a string : ");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{
i++;
}
str[i]='\0';
i--;
j=0;
while(i>=0)
{
4
rev[j]=str[i];
i--;
j++;
}
i=0;
while(str[i]==rev[i]&&str[i]!='\0')
{
i++;
}
if(str[i]=='\0')
{
printf("\ngiven string is palindrome");
}
else
printf("\ngiven string is not palindrome");
getch();
}
Output 1:
Output 2:
Write a program to find the number of ovals and number of consonants and number of
words in a given string
#include<stdio.h>
#include<conio.h>
void main()
{
char str[100];
int i,o,c,w,d;
clrscr();
printf("\nenter a string : ");
gets(str);
o=0;
c=0;
w=1;
i=0;
d=0;
while(str[i]!='\0')
{
5
if(str[i]==' ')
w++;
else if(str[i]>=48&&str[i]<=58)
d++;
else if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||
str[i]=='O'||str[i]=='u'||str[i]=='U')
o++;
else
c++;
i++;
}
printf("\nno of ovals is : %d",o);\
printf("\nno of consonants : %d",c);
printf("\nno of words is : %d",w);
printf("\nno of digits is : %d",d);
getch();
}
Output :
enter a string : work is worship
no of ovals is : 4
no of consonants : 9
no of words is : 3
no of digits is : 0
Every C compiler supports a large number of strings handling library functions. Some
functions listed in below table. All the string handling functions are in-built in <string.h> hearder
file.
6
14 strrchr() Determines last occurrence of a given character in a string
15 strrev() Reverses all characters of a sting
16 strset() Sets all characters of sting with a given arguments or sysmbol
Sets specified numbers of characters of sting with a given argument or
17 strnset() symbol
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[50],str2[50],str3[100];
clrscr();
printf("\nenter the stirng 1 : ");
gets(str1);
printf("\nenter the sting 2 : ");
gets(str2);
printf("\nstring 1 = %s ",str1);
printf("\nstirng 2= %s ",str2);
printf("\nlenght of sting 1 is = %d",strlen(str1));
printf("\nlenght of sting 2 is = %d",strlen(str2));
strcpy(str3,str1);
printf("\ncopy of sting 1 is str3= %s",str3);
strcat(str1,str2);
printf("\nconcatinated sting is str1 = %s",str1);
strset(str3,'*');
printf("\nstr3= %s",str3);
printf("\nreverse of string 1 = %s",strrev(str1));
getch();
}
Output :
7
concatinated sting is str1 = this is c labthis is c program
str3= *************
reverse of string 1 = margorp c si sihtbal c si siht
Write a program to find given string is palindrome or not using sting handling functions
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20],temp[20];
clrscr();
printf("\nenter a string : ");
scanf("%s",str);
strcpy(temp,str);
strrev(str);
if(strcmp(str,temp)==0)
printf("\ngiven string is palindrome ");
else
printf("\ngiven string is nor palindrome ");
getch();
}
Output 1:
Output 2:
8
POINTERS
Variables are used in C to hold data values during the execution of a program. Every
variable when declared occupies certain memory location. In C it is possible to access and
display the address of the memory location of a variable using & operation with variable name.
the Pointer variable is needed to store the memory address of any variable. The pointer is
denoted by ‘*’ asterisk symbol.
Pointer: a Pointer is memory variable that stores a memory address. Pointer can have any
name that is legal for other variable and it is declared in the same fashion like other variables
but it is always denoted by ‘*’ operator. Generally we can say that a Pointer variable hold the
address of other variable.
Above example *a holds the address of integer variable, *f holds the address of float
variable, *c holds the address of character variable.
Features of Pointer:
Write a program to display the address of all variables and pointer variables
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*pa;
char c,*pc;
float f,*pf;
9
clrscr();
pa=&a;
pc=&c;
pf=&f;
printf("\naddress of a = %u",&a);
printf("\naddress of c = %u",&c);
printf("\naddress of f = %u",&f);
printf("\n*** address of pointer varibales ***");
printf("\naddress of a = %u",pa);
printf("\naddress of c = %u",pc);
printf("\naddress of f = %u",pf);
getch();
}
Output:
address of a = 65524
address of c = 65521
address of f = 65514
sizeof(): sizeof functions returns how much of memory had taken by the variable or any data
type in bytes. Its return the integer value i.e memory in bytes of variable.
Note: any type of pointer variable take the 4 bytes of memory allocation in 64-bit operating
system, 2-bytes for 32-bit operating systems.
Write a program to display the size of any data type of pointer variable occupies same
memory in bytes.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\n *** size of datatypes ***");
printf("\n size of int = %d",sizeof(int));
printf("\n size of char = %d",sizeof(char));
printf("\n size of float = %d",sizeof(float));
printf("\n *** size of pointer data types ***");
printf("\n size of pointer of int = %d",sizeof(int*));
printf("\n size of pointer of float = %d",sizeof(float*));
printf("\n size of pointer of char = %d",sizeof(char*));
10
getch();
}
Output:
Generally pointer holds the address the other variable, ordinary variable contains the
value. Pointer wants to hold the address of variable we use ‘&’ at the same to though the pointer
we want to access the variable value we have use the asterisk ‘*’
Example:
int a=10,*pa;
Pa=&a;
Above example ‘a’ is ordinary integer variable, *pa is pointer variable type of integer.
Pa holds the address of ‘a’ using address operator ‘&’, to access the value of ‘a’ i.e a=10
through the pointer we have to use the *pa. and every pointer also contains the address.
Memory allocation
11
Write a program to access the variable value using the pointer variable and display they
value
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*pa;
clrscr();
a=10;
pa=&a;
printf("\n value of a = %d",a);
printf("\n value of a through pointer = %d",*pa);
getch();
}
Output:
value of a = 10
value of a through pointer = 10
Wire a program to perform the arithmetic operations using pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int *pa,*pb;
clrscr();
pa=&a;
pb=&b;
printf("\nenter a and b values : ");
scanf("%d%d",&a,&b);
c=*pa + *pb;
printf("\na+b= %d",c);
c=*pa - *pb;
printf("\na-b= %d",c);
c=*pa * *pb;
printf("\na*b= %d",c);
c=*pa / *pb;
printf("\na/b= %d",c);
c=*pa % *pb;
printf("\n mod = %d",c);
getch();
}
Output :
12
enter a and b values : 10 3
a+b= 13
a-b= 7
a*b= 30
a/b= 3
mod = 1
Write a program to perform increment or decrement operations using pointer and show
the variation of address while increment and decrement of the pointer.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
int *pa;
clrscr();
pa=&a;
a=10;
printf("\nvalue accessing through pointer");
++*pa;
printf("\na = %d",a);
--*pa;
printf("\na = %d",a);
*pa++;
printf("\na = %d",a);
*pa--;
printf("\na = %d",a);
printf("\naddress operation through pointer ");
printf("\naddress of pa = %u",pa);
++pa;
printf("\naddress after preincrement = %u",pa);
pa++;
printf("\naddress after postincrement = %u",pa);
pa--;
printf("\naddress after postdecrement = %u",pa);
--pa;
printf("\naddress after predecremnet = %u",pa);
getch();
}
Output
14
Arrays using pointer:
Array name itself is an address or pointer. It points to the address of the first element (0 th
element of any array). The elements of the array together with their address can be displayed by
using array name itself. Array elements are always stored to contiguous memory locations.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={5,10,20,25,30};
int i;
clrscr();
printf("\nvalues and address of array elaments ");
for(i=0;i<5;i++)
{
printf("\na[%d] = %d\t address= %u",i,a[i],&a[i]);
}
getch();
}
Output:
Write a program to access the array elements using the pointer variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={5,10,20,25,30};
int i,*p;
clrscr();
p=&a[0];
for(i=0;i<5;i++)
{
printf("\na[%d] = %d",i,*p);
++p; // address increment
}
15
getch();
}
Output :
a[0] = 5
a[1] = 10
a[2] = 20
a[3] = 25
a[4] = 30
16