0% found this document useful (0 votes)
12 views10 pages

Module4 Notes

This document discusses strings and structures in C programming. It covers: 1) Different ways to declare and initialize strings, including character arrays, string constants, and pointers. 2) Common string library functions like strlen(), strcpy(), strcat(), and strcmp(). 3) Two-dimensional character arrays (arrays of strings). 4) Arrays of pointers to strings. 5) What structures are and how to declare a structure with multiple data types to store related data, like the elements of a book.

Uploaded by

soumyaks81
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
12 views10 pages

Module4 Notes

This document discusses strings and structures in C programming. It covers: 1) Different ways to declare and initialize strings, including character arrays, string constants, and pointers. 2) Common string library functions like strlen(), strcpy(), strcat(), and strcmp(). 3) Two-dimensional character arrays (arrays of strings). 4) Arrays of pointers to strings. 5) What structures are and how to declare a structure with multiple data types to store related data, like the elements of a book.

Uploaded by

soumyaks81
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 10

Module IV - Strings and Structures

4.1 To understand Strings


4.1.1 To know about strings
4.1.2 To know about standard Library String Functions
4.1.3 To know about Two- Dimensional Array of Characters
4.1.4 To know array of Pointers to Strings
4.2 To understand Structures
4.2.1 To know about Structures
4.2.2 To discuss Array of Structures

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

In short Strings can be declared and initialized in the following ways:


1. Declare a string and input using scanf()

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()

Use of pointers to access array elements


main( ) {
char name[ ] = "Welcome" ; char *ptr ;
ptr = name ; /* ptr will store starting address of name ie. ptr= 65510 */
while ( *ptr != `\0' ) { Output:
printf( "%c", *ptr ) ; Welcome
ptr++ ;} }

Standard Library String Functions


Most commonly used string library functions are:
1. strlen()
 Used to find the length of a string. It counts the number of characters present in
astring.
Example:
main( ) { Output:
char arr[ ] = "Bamboozled" ;
int len1, len2 ; string = Bamboozled length = 10
len1 = strlen( arr ) ; string = Humpty Dumpty length = 13
len2 = strlen( "Humpty Dumpty" ) ;
printf( "\nstring = %s length = %d", arr, len1 ) ;
printf( "\nstring = %s length = %d", "Humpty Dumpty", len2 ) ;
}

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:

source string = Sayonara

target string = Sayonara


char str2[20] ;
strcpy( str2, str1 ) ;
printf( "\nsource string = %s", str1 ) ;
printf( "\ntarget string = %s", str2 ) ;
}

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.

Two-Dimensional Array of Characters(array of strings)


Initialize Two-Dimensional Array of Characters
Example: Here names is a 2-Dimensional array which has 4 names , each contains an array of
characters.
5
The first subscript gives the number of names in the array, while the second subscript gives the
length of each item in the array(or the no of. characters in each name).
main( ) {
char names[4][10] = { "akshay",
"parag",
"raman",
"srinivas”
};
The names would be stored in the memory as shown in Figure

Array of Pointers to Strings(more convenient to use with two dim array of


characters or array of strings)
Pointers are variables which contains addresses. Array of pointers to string means an array of
memory addresses which contains base addresses (starting address) of respective names.
That is, base address of “akshay” is stored in names[0], base address of “parag” is stored in
names[1] and so on.
The following declaration shows an array named ‘names’ which is a collection of pointers.
char *names[ ] = {
"akshay",
"parag",
"raman",
"srinivas"
};
The memory representation of this array is shown below:
names[]

1020 2340 185 3412


6
65510 65511 65512 65513

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

printf ( "\nAnd this is what you entered" ) ;


printf ( "\n%s %f %d", b1.name, b1.price, b1.pages ) ;
printf ( "\n%s %f %d", b2.name, b2.price, b2.pages ) ;
printf ( "\n%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.

/* Usage of an array of structures */


main( )
{
struct book
{
char name[25] ;
float price ;
int pages ;
};
struct book b[100] ;

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

You might also like