0% found this document useful (0 votes)
20 views16 pages

Strings and Pointers.pdf

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
20 views16 pages

Strings and Pointers.pdf

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

STRINGS

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

Syntax: char variable_name[index];

Declaration and Initialization

char string[] = {"ANANATAPURAMU"};

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

St1[0] St1[1] St1[2] St1[3] St1[4] St1[5] St1[6]

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

Display of string with different formats

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

read the string using scanf : this is c program

string from gets str1=this is c program


string from scanf str2=this

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:

enter a string : anantapur


given string is not palindrome

Output 2:

enter a string : malayalam


given string is palindrome

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

String functions / String handling functions

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.

s.no Function Description


1 strlen() Determines length of sting
2 strcpy() Copies a string from source to destination
3 strncpy() Copies characters of a sting to another sting upto the specified length
4 strcmp() Compares two string (function discriminates between small & capital letters .
Compares two string (function does not discriminates between small &
5 stricmp() capital letters.
6 strncmp() Compares two strings characters upto specified length (case sensitive)
7 strnicmp() Compares two strings characters upto specified length (not case sensitive)
8 strlwr() Converts uppers case characters into lower case characters
9 strupr() Converts lower case characters into upper case characters
10 strdup() Duplicate string
11 strcat() Append source string to destinations string
12 strstr() Determines first occurrence of a given string in another string
13 strchr() Determines first occurrence of a given character in a string

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

Write a program to implement the string handling functions

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

enter the stirng 1 : this is c lab

enter the sting 2 : this is c program

string 1 = this is c lab


stirng 2= this is c program
lenght of sting 1 is = 13
lenght of sting 2 is = 17
copy of sting 1 is str3= this is c lab

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:

enter a string : india


given string is nor palindrome

Output 2:

enter a string : ece


given string is palindrome

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.

Declaration of Pointer variable:

Pointer variable declaration follows the given sysntax


Sysntax:
Data_type *variable_name;
Example:
int *a;
float *f;
char *c;

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:

1. Pointers save the memory space.


2. Execution time with the pointer is faster because data is manipulated with the address
i.e direct access to memory location.
3. The memory is accessed efficiently with the pointer. The pointer assigns the memory
space and it also releases. Dynamically memory is allocated.
4. Pointers are used with data structures. They are useful for representing two dimensional
and multi-dimensional arrays.

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

*** address of pointer varibales ***


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:

*** size of datatypes ***


size of int = 2
size of char = 1
size of float = 4
*** size of pointer data types ***
size of pointer of int = 2
size of pointer of float = 2
size of pointer of char = 2

Accessing the variable through the pointer

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.

Figure shows the pointers in memory allocations.

Memory allocation

5082 🡨 address of ‘a’ and a=10


.
.
.
.
.
6084 🡨 address of *pa and pa=5082
.
..

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

value accessing through pointer


a = 11
a = 10
13
a = 10
a = 10
address operation through pointer
address of pa = 65524
address after preincrement = 65526
address after postincrement = 65528
address after postdecrement = 65526
address after predecremnet = 65524

5082 🡨 address of ‘a’ and a=10


.
.
.
.
.
6084 🡨 address of *pa and pa=5082, *pa=10
.
Pa++
6086..
🡨 address of *pa and pa=0, *pa=0

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.

Write a program to display the address of all array elements

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

values and address of array elaments


a[0] = 5 address= 65516
a[1] = 10 address= 65518
a[2] = 20 address= 65520
a[3] = 25 address= 65522
a[4] = 30 address= 65524

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

Pointers using functions: refer call by reference in the functions

16

You might also like