Module4 Notes
Module4 Notes
Strings
Strings are character arrays.
A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ).
For example,
char name[ ] = { 'W', 'E', 'L', 'C', 'O', 'M', 'E', '\0' } ;
Each character in the array occupies one byte of memory and the last character is always ‘\0’,
which is called null character. It indicates where the string ends.
The memory storage of the character array is shown below:
W E L C O M E \O
65510 65511 65512 65513 65514 65515 65516 65517
The above string can also be initialized as
Char name[]=”WELCOME”;
main( )
{ char name[25] ;
1
printf ( "Enter your name " ) ;
scanf ( "%s", name ) ; // Read the string
sprintf ( "The name is %s", name ) ;
}
2. Declare and initialize a string constant
Program: To display string
main( ) { The output is
char name[ ] = "Welcome" ; Welcome
int i = 0 ;
//display the string by printing the characters one by one until end of string(‘\0’)
while(name[i] != `\0' )
{
printf( "%c", name[i] ) ;
i++ ;
}
}
3. Declare and initialize with individual characters . Note that, the last character must
be ‘\0’
char name[ ] = { 'W', 'E', 'L', 'C', 'O', 'M', 'E', '\0' } ;
4. Declare and input using gets()
scanf( ) is not capable of receiving multi-word strings. Therefore names such as
‘Debashish Roy’ would be unacceptable.(Space not accepted).
puts() is used to display the string
main( )
{ char name[25] ;
printf ( "Enter your full name " ) ;
gets ( name ) ;
puts ( "Hello!" ) ;
puts ( name ) ; }
2
Output:
Enter your name Debashish Roy
Hello!
Qstn: write how to declare string and write
methods to read the string Debashish Roy
Ans.. the above methods 1 and 2 and using gets()
2. strcpy()
Copy the contents of one string to another.
Syntax: strcpy(target,source);
Example: strcpy(str1,str2); // will copy str2 to str1
Program:
main( ) {
char str1[ ] = "Sayonara" ;
3 Output:
3. strcat()
Concatenates the source string at the end of target string.
Syntax: strcat(target,source);
Example:
main( ) {
char sourcestr[ ] = "Folks!" ; Output:
char targetstr[30] = "Hello" ; source string = Folks!
strcat( targetstr, sourcestr ) ; target string = HelloFolks!
printf( "\nsource string = %s",
sourcestr ) ;
printf( "\ntarget string = %s", targetstr ) ;
}
4. strcmp()
Compares two strings to find whether they are same or not.
The two strings are compared character by character until there is a mismatch or
end of one of the strings is reached, whichever occurs first.
If the two strings are identical, strcmp( ) returns a value zero.
If they’re not, it returns the numeric difference between the ASCII values of the
first non-matching pairs of characters.
Syntax: strcmp(str1,str2);
Example;
main( ) {
char string1[ ] = "Jerry" ;
char string2[ ] = "Ferry" ;
4
int i, j, k ;
i = strcmp( string1, "Jerry" ) ;
j = strcmp( string1, string2 ) ;
k = strcmp( string1, "Jerry boy" ) ;
printf( "\n%d %d %d", i, j, k ) ;
}
The string functions are listed below.
Akshay\0 Parag\0
1020 2340
Srinivas\0
Raman\0
185 3412
Structures
A structure contains a number of data types grouped together. These data types may or may not
be of the same type.
(We have seen earlier how ordinary variables can hold one piece of information and how arrays
can hold a number of pieces of information of the same data type)
For eg suppose you want to store data about a book. You might want to store its name (a string),
its price (a float) and number of pages in it (an int).The structure variable can be used to store the
data about a book.
Declaring a Structure
struct <structure name>
struct book { structure element 1 ;
structure element 2 ; structure
{ char name[10] ; element 3 ; ...... ......
float price ; ……..
int pages ; };
};
Once the new structure data type has been defined one or more variables can be declared to be of
that type. For example the variables b1, b2, b3 can be declared to be of the type struct book, as,
struct book b1, b2, b3 ;
There are three ways to declare struct all are similar
1. Method1
struct book
{ char name[10] ;
float price ;
int pages ;
7
};
struct book b1, b2, b3 ;
2. method2
struct book
{ char name ;
float price ;
int pages ;
} b1, b2, b3 ;
3. Method3
struct
{ char name ;
float price ;
int pages ;
} b1, b2, b3 ;
Initialise a structure
structure variables can also be initialized where they are declared.
struct book
{ char name[10] ;
float price ;
int pages ;
};
struct book b1 = { "Basic", 130.00, 550 } ;
struct book b2 = { "Physics", 150.80, 800 } ;
Accessing Structure Elements
In arrays we can access individual elements of an array using a subscript.
Structures use a different scheme. They use a dot (.) operator.
So to refer to pages of the structure defined in our sample program we have to use,
b1.pages
Similarly, to refer to price we would use, b1.price
8
main( )
{
struct book
{ char name[10] ;
float price ;
int pages ;
};
struct book b1, b2, b3 ;
printf ( "\nEnter names, prices & no. of pages of 3 books\n" ) ;
scanf ( "%s %f %d", &b1.name, &b1.price, &b1.pages ) ;
scanf ( "%s %f %d", &b2.name, &b2.price, &b2.pages ) ;
scanf ( "%s %f %d", &b3.name, &b3.price, &b3.pages ) ;
Array of Structures
In our sample program, to store data of 100 books we would be required to use 100 different
structure variables from b1 to b100, which is definitely not very convenient. A better approach
would be to use an array of structures.
9
int i ;
//read details of books
for ( i = 0 ; i <= 99 ; i++ )
{
printf ( "\nEnter name, price and pages " ) ;
scanf ( "%s %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
}
//display details of books
for ( i = 0 ; i <= 99 ; i++ )
printf ( "\n%s %f %d", b[i].name, b[i].price, b[i].pages ) ;
}
10