SlideShare a Scribd company logo
1
1
What is a pointer?
 First of all, it is a variable, just like other variables you
studied
 So it has type, storage etc.
 Difference: it can only store the address (rather than the
value) of a data item
 Type of a pointer variable – pointer to the type of the data
whose address it will store
 Example: int pointer, float pointer,…
 Can be pointer to any user-defined types also like structure types
2
2
Values vs Locations
 Variables name memory locations, which hold values
32
x
1024:
address
name
value
3
3
Contd.
 Consider the statement
int xyz = 50;
This statement instructs the compiler to
allocate a location for the integer variable xyz,
and put the value 50 in that location
Suppose that the address location chosen is
1380 xyz  variable
50  value
1380  address
4
4
Contd.
 During execution of the program, the system always
associates the name xyz with the address 1380
 The value 50 can be accessed by using either the
name xyz or the address 1380
 Since memory addresses are simply numbers, they
can be assigned to some variables which can be
stored in memory
 Such variables that hold memory addresses are
called pointers
 Since a pointer is a variable, its value is also stored
in some memory location
5
5
Contd.
 Suppose we assign the address of xyz to a
variable p
 p is said to point to the variable xyz
Variable Value Address
xyz 50 1380
p 1380 2545
p = &xyz;
*p=xyz (50)
6
6
Pointers
A pointer is just a C variable whose value can contain the
address of another variable
Needs to be declared before use just like any other variable
General form:
data_type *pointer_name;
Three things are specified in the above declaration:
 The asterisk (*) tells that the variable pointer_name is a
pointer variable
 pointer_name needs a memory location
 pointer_name points to a variable of type data_type
7
7
Example
int *count;
float *speed;
char *c;
 Once a pointer variable has been declared, it can be made
to point to a variable using an assignment statement like
int *p, xyz;
:
p = &xyz;
 This is called pointer initialization
8
Strings
9
Strings
• 1-d arrays of type char
• By convention, a string in C is terminated by the
end-of-string sentinel ‘0’ (null character)
• char s[21] - can have variable length string
delimited with 0
• Max length of the string that can be stored is 20 as the
size must include storage needed for the ‘0’
• String constants : “hello”, “abc”
• “abc” is a character array of size 4
10
Character Arrays and Strings
char C[8] = { 'a', 'b', 'h', 'i', 'j', 'i', 't', '0' };
 C[0] gets the value 'a', C[1] the value 'b', and so on. The last (7th)
location receives the null character ‘0’
 Null-terminated (last character is ‘0’) character arrays are also called
strings
 Strings can be initialized in an alternative way. The last declaration is
equivalent to:
char C[8] = "abhijit";
 The trailing null character is missing here. C automatically puts it at
the end if you define it like this
 Note also that for individual characters, C uses single quotes,
whereas for strings, it uses double quotes
11
Reading strings: %s format
void main()
{
char name[25];
scanf("%s", name);
printf("Name = %s n", name);
}
%s reads a string into a character array
given the array name or start address.
It ends the string with ‘0’
12
An example
void main()
{
#define SIZE 25
int i, count=0;
char name[SIZE];
scanf("%s", name);
printf("Name = %s n", name);
for (i=0; name[i]!='0'; i++)
if (name[i] == 'a') count++;
printf("Total a's = %dn", count);
}
Satyanarayana
Name = Satyanarayana
Total a's = 6
Note that character strings read
in %s format end with ‘0’
Seen on screen
Typed as input
Printed by program
13
Differences : array & pointers
char *p = “abcde”;
The compiler allocates
space for p, puts the
string constant “abcde”
in memory somewhere
else, initializes p with
the base address of
the string constant
char s[ ] = “abcde”;
 char s[ ] = {‘a’,’b’,’c’,’d’,’e’.’0’};
The compiler allocates 6 bytes
of memory for the array s
which are initialized with the
6 characters
a b c d e 0
a b c d e 0
p
S
14
String Constant
• A string constant is treated as a pointer
• Its value is the base address of the string
char *p = “abc”;
printf (“%s %sn”,p,p+1); /* abc bc is printed */
a b c 0
p
15
Library Functions for String
Handling
 You can write your own C code to do different
operations on strings like finding the length of a
string, copying one string to another, appending
one string to the end of another etc.
 C library provides standard functions for these
that you can call, so no need to write your own
code
 To use them, you must do
#include <string.h>
At the beginning of your program (after #include
<stdio.h>)
16
String functions we will see
 strlen : finds the length of a string
 strcat : concatenates one string at the end
of another
 strcmp : compares two strings
lexicographically
 strcpy : copies one string to another
17
strlen()
int strlen(const char *s)
 Takes a null-terminated
strings (we routinely refer
to the char pointer that
points to a null-terminated
char array as a string)
 Returns the length of
the string, not counting
the null (0) character
int strlen (const char *s) {
int n;
for (n=0; *s!=‘0’; ++s)
++n;
return n;
}
You cannot change contents
of s in the function
18
strcat()
 char *strcat (char *s1,
const char *s2);
 Takes 2 strings as
arguments,
concatenates them,
and puts the result in
s1. Returns s1.
Programmer must
ensure that s1 points
to enough space to
hold the result.
char *strcat(char *s1, const char
*s2)
{
char *p = s1;
while (*p != ‘0’) /* go to end */
++p;
while(*s2 != ‘0’)
*p++ = *s2++; /* copy */
*p = ‘0’;
return s1;
}
You cannot change contents
of s2 in the function
19
strcmp()
int strcmp (const char
*s1, const char *s2);
Two strings are passed
as arguments. An
integer is returned
that is less than,
equal to, or greater
than 0, depending
on whether s1 is
lexicographically
less than, equal to,
or greater than s2.
20
strcmp()
int strcmp (const char
*s1, const char *s2);
Two strings are passed
as arguments. An
integer is returned
that is less than,
equal to, or greater
than 0, depending
on whether s1 is
lexicographically
less than, equal to,
or greater than s2.
int strcmp(char *s1, const char *s2)
{
for (;*s1!=‘0’&&*s2!=‘0’; s1++,s2++)
{
if (*s1>*s2) return 1;
if (*s2>*s1) return -1;
}
if (*s1 != ‘0’) return 1;
if (*s2 != ‘0’) return -1;
return 0;
}
21
char *strcpy (char *s1, char *s2);
The characters is the string s2 are copied into s1
until 0 is moved. Whatever exists in s1 is
overwritten. It is assumed that s1 has enough space
to hold the result. The pointer s1 is returned.
strcpy()
22
char *strcpy (char *s1, const char *s2);
The characters is the string s2 are copied into s1 until
‘0’ is moved. Whatever exists in s1 is overwritten. It
is assumed that s1 has enough space to hold the
result. The pointer s1 is returned.
char * strcpy (char *s1, const char *s2)
{
char *p = s1;
while (*p++ = *s2++) ;
return s1;
}
strcpy()
23
Example: Using string functions
25
9
-1
big sky country
beautiful brown cows!
int main()
{
char s1[ ] = "beautiful big sky country",
s2[ ] = "how now brown cow";
printf("%dn",strlen (s1));
printf("%dn",strlen (s2+8));
printf("%dn", strcmp(s1,s2));
printf("%sn",s1+10);
strcpy(s1+10,s2+8);
strcat(s1,"s!");
printf("%sn", s1);
return 0;
}
Output

More Related Content

DOCX
Unitii string
Sowri Rajan
 
PPT
strings
teach4uin
 
PDF
Unit 2
TPLatchoumi
 
PDF
String notes
Prasadu Peddi
 
PPTX
introduction to strings in c programming
mikeymanjiro2090
 
PPT
Unit-3 Strings.pptreeeeeeeeeeeeeereeeeere
edukuldeep2005
 
PPTX
Week6_P_String.pptx
OluwafolakeOjo
 
DOCX
C Programming Strings.docx
8759000398
 
Unitii string
Sowri Rajan
 
strings
teach4uin
 
Unit 2
TPLatchoumi
 
String notes
Prasadu Peddi
 
introduction to strings in c programming
mikeymanjiro2090
 
Unit-3 Strings.pptreeeeeeeeeeeeeereeeeere
edukuldeep2005
 
Week6_P_String.pptx
OluwafolakeOjo
 
C Programming Strings.docx
8759000398
 

Similar to string function with example................... (20)

PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPT
string.ppt
lakshmanarao027MVGRC
 
PPTX
String in programming language in c or c++
Azeemaj101
 
PPTX
Pointers
PreethyJemima
 
PPT
Strings
Mitali Chugh
 
PPTX
cprogramming strings.pptx
LECO9
 
PPTX
cprogramming strings.pptx
SKUP1
 
PDF
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
PPT
Unit3 C
arnold 7490
 
PPTX
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
PPTX
C Programming Unit-3
Vikram Nandini
 
DOCX
Array &strings
UMA PARAMESWARI
 
PPTX
Strings in C - covers string functions
Mohammed Sikander
 
PPT
C++ Strings.ppt
DilanAlmsa
 
PPT
String & its application
Tech_MX
 
PPTX
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
 
PPTX
Array, string and pointer
Nishant Munjal
 
PDF
Chapter 3 - Characters and Strings - Student.pdf
mylinhbangus
 
PPTX
Handling of character strings C programming
Appili Vamsi Krishna
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
String in programming language in c or c++
Azeemaj101
 
Pointers
PreethyJemima
 
Strings
Mitali Chugh
 
cprogramming strings.pptx
LECO9
 
cprogramming strings.pptx
SKUP1
 
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
Unit3 C
arnold 7490
 
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
C Programming Unit-3
Vikram Nandini
 
Array &strings
UMA PARAMESWARI
 
Strings in C - covers string functions
Mohammed Sikander
 
C++ Strings.ppt
DilanAlmsa
 
String & its application
Tech_MX
 
Bsc cs i pic u-4 function, storage class and array and strings
Rai University
 
Array, string and pointer
Nishant Munjal
 
Chapter 3 - Characters and Strings - Student.pdf
mylinhbangus
 
Handling of character strings C programming
Appili Vamsi Krishna
 
Ad

Recently uploaded (20)

PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
mansk2
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
High Ground Student Revision Booklet Preview
jpinnuck
 
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
Week 4 Term 3 Study Techniques revisited.pptx
mansk2
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Landforms and landscapes data surprise preview
jpinnuck
 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
High Ground Student Revision Booklet Preview
jpinnuck
 
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Ad

string function with example...................

  • 1. 1 1 What is a pointer?  First of all, it is a variable, just like other variables you studied  So it has type, storage etc.  Difference: it can only store the address (rather than the value) of a data item  Type of a pointer variable – pointer to the type of the data whose address it will store  Example: int pointer, float pointer,…  Can be pointer to any user-defined types also like structure types
  • 2. 2 2 Values vs Locations  Variables name memory locations, which hold values 32 x 1024: address name value
  • 3. 3 3 Contd.  Consider the statement int xyz = 50; This statement instructs the compiler to allocate a location for the integer variable xyz, and put the value 50 in that location Suppose that the address location chosen is 1380 xyz  variable 50  value 1380  address
  • 4. 4 4 Contd.  During execution of the program, the system always associates the name xyz with the address 1380  The value 50 can be accessed by using either the name xyz or the address 1380  Since memory addresses are simply numbers, they can be assigned to some variables which can be stored in memory  Such variables that hold memory addresses are called pointers  Since a pointer is a variable, its value is also stored in some memory location
  • 5. 5 5 Contd.  Suppose we assign the address of xyz to a variable p  p is said to point to the variable xyz Variable Value Address xyz 50 1380 p 1380 2545 p = &xyz; *p=xyz (50)
  • 6. 6 6 Pointers A pointer is just a C variable whose value can contain the address of another variable Needs to be declared before use just like any other variable General form: data_type *pointer_name; Three things are specified in the above declaration:  The asterisk (*) tells that the variable pointer_name is a pointer variable  pointer_name needs a memory location  pointer_name points to a variable of type data_type
  • 7. 7 7 Example int *count; float *speed; char *c;  Once a pointer variable has been declared, it can be made to point to a variable using an assignment statement like int *p, xyz; : p = &xyz;  This is called pointer initialization
  • 9. 9 Strings • 1-d arrays of type char • By convention, a string in C is terminated by the end-of-string sentinel ‘0’ (null character) • char s[21] - can have variable length string delimited with 0 • Max length of the string that can be stored is 20 as the size must include storage needed for the ‘0’ • String constants : “hello”, “abc” • “abc” is a character array of size 4
  • 10. 10 Character Arrays and Strings char C[8] = { 'a', 'b', 'h', 'i', 'j', 'i', 't', '0' };  C[0] gets the value 'a', C[1] the value 'b', and so on. The last (7th) location receives the null character ‘0’  Null-terminated (last character is ‘0’) character arrays are also called strings  Strings can be initialized in an alternative way. The last declaration is equivalent to: char C[8] = "abhijit";  The trailing null character is missing here. C automatically puts it at the end if you define it like this  Note also that for individual characters, C uses single quotes, whereas for strings, it uses double quotes
  • 11. 11 Reading strings: %s format void main() { char name[25]; scanf("%s", name); printf("Name = %s n", name); } %s reads a string into a character array given the array name or start address. It ends the string with ‘0’
  • 12. 12 An example void main() { #define SIZE 25 int i, count=0; char name[SIZE]; scanf("%s", name); printf("Name = %s n", name); for (i=0; name[i]!='0'; i++) if (name[i] == 'a') count++; printf("Total a's = %dn", count); } Satyanarayana Name = Satyanarayana Total a's = 6 Note that character strings read in %s format end with ‘0’ Seen on screen Typed as input Printed by program
  • 13. 13 Differences : array & pointers char *p = “abcde”; The compiler allocates space for p, puts the string constant “abcde” in memory somewhere else, initializes p with the base address of the string constant char s[ ] = “abcde”;  char s[ ] = {‘a’,’b’,’c’,’d’,’e’.’0’}; The compiler allocates 6 bytes of memory for the array s which are initialized with the 6 characters a b c d e 0 a b c d e 0 p S
  • 14. 14 String Constant • A string constant is treated as a pointer • Its value is the base address of the string char *p = “abc”; printf (“%s %sn”,p,p+1); /* abc bc is printed */ a b c 0 p
  • 15. 15 Library Functions for String Handling  You can write your own C code to do different operations on strings like finding the length of a string, copying one string to another, appending one string to the end of another etc.  C library provides standard functions for these that you can call, so no need to write your own code  To use them, you must do #include <string.h> At the beginning of your program (after #include <stdio.h>)
  • 16. 16 String functions we will see  strlen : finds the length of a string  strcat : concatenates one string at the end of another  strcmp : compares two strings lexicographically  strcpy : copies one string to another
  • 17. 17 strlen() int strlen(const char *s)  Takes a null-terminated strings (we routinely refer to the char pointer that points to a null-terminated char array as a string)  Returns the length of the string, not counting the null (0) character int strlen (const char *s) { int n; for (n=0; *s!=‘0’; ++s) ++n; return n; } You cannot change contents of s in the function
  • 18. 18 strcat()  char *strcat (char *s1, const char *s2);  Takes 2 strings as arguments, concatenates them, and puts the result in s1. Returns s1. Programmer must ensure that s1 points to enough space to hold the result. char *strcat(char *s1, const char *s2) { char *p = s1; while (*p != ‘0’) /* go to end */ ++p; while(*s2 != ‘0’) *p++ = *s2++; /* copy */ *p = ‘0’; return s1; } You cannot change contents of s2 in the function
  • 19. 19 strcmp() int strcmp (const char *s1, const char *s2); Two strings are passed as arguments. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2.
  • 20. 20 strcmp() int strcmp (const char *s1, const char *s2); Two strings are passed as arguments. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2. int strcmp(char *s1, const char *s2) { for (;*s1!=‘0’&&*s2!=‘0’; s1++,s2++) { if (*s1>*s2) return 1; if (*s2>*s1) return -1; } if (*s1 != ‘0’) return 1; if (*s2 != ‘0’) return -1; return 0; }
  • 21. 21 char *strcpy (char *s1, char *s2); The characters is the string s2 are copied into s1 until 0 is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer s1 is returned. strcpy()
  • 22. 22 char *strcpy (char *s1, const char *s2); The characters is the string s2 are copied into s1 until ‘0’ is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer s1 is returned. char * strcpy (char *s1, const char *s2) { char *p = s1; while (*p++ = *s2++) ; return s1; } strcpy()
  • 23. 23 Example: Using string functions 25 9 -1 big sky country beautiful brown cows! int main() { char s1[ ] = "beautiful big sky country", s2[ ] = "how now brown cow"; printf("%dn",strlen (s1)); printf("%dn",strlen (s2+8)); printf("%dn", strcmp(s1,s2)); printf("%sn",s1+10); strcpy(s1+10,s2+8); strcat(s1,"s!"); printf("%sn", s1); return 0; } Output