0% found this document useful (0 votes)
16 views18 pages

Strings

String
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

Strings

String
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

String

UNIT 3
What are Strings?

 Character arrays are many a time also called strings.


 Character arrays or strings are used by programming
languages to manipulate text, such as words and
sentences
 A string constant is a one-dimensional array of
characters terminated by a null ( ‟\0‟ ).
 For example,
 char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;
 Each character in the array occupies 1 byte of
memory and the last character is always ‟\0‟.
String

 ‟\0‟ is called null character. Note that ‟\0‟ and ‟0‟ are
not same. ASCII value of ‟\0‟ is 0, whereas ASCII
value of ‟0‟ is 48.
 The terminating null (‟\0‟) is important, because it is
the only way the functions that work with a string
can know where the string ends.
 In fact, a string not terminated by a ‟\0‟ is not really
a string, but merely a collection of characters.
 A shortcut for initializing strings is
char name[ ] = "HAESLER" ;
Note that, in this declaration ‟\0‟ is not necessary. C
inserts the null character automatically.
Examples

 # include <stdio.h>  # include <stdio.h>


 int main( )  int main( )
 {  {
 char name[ ] = {„R‟,‟A‟,‟M‟,‟A‟,‟\0‟} ;  char name[ ] = {„R‟,‟A‟,‟M‟,‟A‟,‟\0‟} ;
 int i = 0 ;  int i = 0 ;
 while ( i <= 4 )  while (name[ i ] != '\0')
 {  {
 printf ( "%c", name[ i ] ) ;  printf ( "%c", name[ i ] ) ;
 i++ ;  i++ ;
 }  }
 printf ( "\n" ) ;  printf ( "\n" ) ;
 return 0 ;  return 0 ;
 }  }

 2nd way to print all characters without


loop is
 printf(“%s”,name); //need /n to change
the line
 Puts(name); // automatically changes the
line
Examples



int main( )
{
 # include <stdio.h>


char name[ ] =“rama” ;
int i = 0 ;
 int main( )


while (name[ i ] != '\0')
{
 {


printf ( "%c", name[ i ] ) ;
i++ ;
 char name[ 25 ] ;
 }
printf ( "\n" ) ;
 printf ( "Enter your name "

 return 0 ; );
}

 scanf ( "%s", name ) ;
 printf ( "Hello %s!\n",
 The %s used in printf( ) is name ) ;
a format specification for
printing out a string  return 0 ;
 }
Point to remember

 the scanf( ) function fills in the characters typed


 at keyboard into the array until the Enter key is hit. Once
enter is hit, scanf( ) places a ‟\0‟ in the array.
 scanf( ) is not capable of receiving multi-word strings.
Therefore, names such as „Debashish Roy‟ would be
unacceptable.
 The length of the string should not exceed the dimension
of the character array. This is because the C compiler
doesn‟t perform bounds checking on character arrays.
Hence, if you carelessly exceed the bounds, there is
always a danger of overwriting something important
Get( ) & put( )

 The way to get around the limitation is by using the


function gets( ).
# include <stdio.h>
int main( )
{
char name[ 25 ] ;
printf ( "Enter your full name: " ) ;
gets ( name ) ;
puts ( "Hello!" ) ;
puts ( name ) ;
return 0 ;
}
 The program and the output are self-explanatory
except for the fact that, puts( ) can display only one
string at a time (hence the use of two puts( ) in the
program above).
 Also, on displaying a string, unlike printf( ), puts(
) places the cursor on the next line.
 Though gets( ) is capable of receiving only one
string at a time, the plus point with gets( ) is that it
can receive a multi-word string.
Point to Remember

 If we are prepared to take the trouble, we can make


scanf( ) accept multi-word strings by writing it in this
manner:
char name[ 25 ] ;
printf ( "Enter your full name " ) ;
scanf ( "%[ ^\n ]s", name ) ;
 Here, [^\n] indicates that scanf( ) will keep receiving
characters into name[ ] until \n is encountered.
 Though workable, this is not thebest of the ways to call a
function, you would agree.
SORTING

 BUBBLE SORT
Bubble Sort is the simplest sorting algorithm that works by
repeatedly swapping the adjacent elements if they are in
the wrong order
 Input: arr[] = {6, 3, 0, 5}
 First Pass:
 Bubble sort starts with very first two elements,
comparing them to check which one is greater.
 ( 6 3 0 5 ) –> ( 3 6 0 5 ), Here, algorithm compares the first two
elements, and swaps since 6 > 3.
 ( 3 6 0 5 ) –> ( 3 0 6 5 ), Swap since 6 > 0
 ( 3 0 6 5 ) –> ( 3 0 5 6 ), Swap since 6 > 5
Bubble Sort

 Now, during second iteration it should look like this:


 ( 3 0 5 6 ) –> ( 0 3 5 6 ), Swap since 3 > 0

 ( 0 3 5 6 ) –> ( 0 3 5 6 ), No change as 5 > 3

 Third Pass:
 Now, the array is already sorted, but our algorithm
does not know if it is completed.
 The algorithm needs one whole pass without any
swap to know it is sorted.
 ( 0 3 5 6 ) –> ( 0 3 5 6 ), No change as 3 > 0
Insertion Sort

 Consider an example: arr[]: {12, 11, 13, 5, 6}


 12 11 13 5 6

 First Pass:
Initially, the first two elements of the array are compared in insertion
sort.

 12 11 13 5 6

Here, 12 is greater than 11 hence they are not in the ascending order
and 12 is not at its correct position. Thus, swap 11 and 12.
So, for now 11 is stored in a sorted sub-array.

 11 12 13 5 6
 Now, move to the next two elements and compare them

 11 12 13 5 6

 Here, 13 is greater than 12, thus both elements seems to be in ascending order, hence, no swapping will
occur. 12 also stored in a sorted sub-array along with 11

 Third Pass:

 Now, two elements are present in the sorted sub-array which are 11 and 12
 Moving forward to the next two elements which are 13 and 5

 11 12 13 5 6

 Both 5 and 13 are not present at their correct place so swap them

 11 12 5 13 6

 After swapping, elements 12 and 5 are not sorted, thus swap again

 11 5 12 13 6

 Here, again 11 and 5 are not sorted, hence swap again

 5 11 12 13 6

 Here, 5 is at its correct position

 Fourth Pass:

 Now, the elements which are present in the sorted sub-array are 5, 11 and 12
 Moving to the next two elements 13 and 6

 5 11 12 13 6
Selection Sort

 Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the
smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion
of the list
 Lets consider the following array as an example: arr[] = {64, 25, 12, 22, 11}

 First pass:

 For the first position in the sorted array, the whole array is traversed from index 0 to 4 sequentially.
The first position where 64 is stored presently, after traversing whole array it is clear that 11 is the
lowest value.

 64 25 12 22 11

 Thus, replace 64 with 11. After one iteration 11, which happens to be the least value in the array,
tends to appear in the first position of the sorted list.

 11 25 12 22 64
 Second Pass:

 For the second position, where 25 is present, again


traverse the rest of the array in a sequential manner.

 11 25 12 22 64

 After traversing, we found that 12 is the second lowest


value in the array and it should appear at the second
place in the array, thus swap these values.

 11 12 25 22 64
 Third Pass:

 Now, for third place, where 25 is present again traverse the


rest of the array and find the third least value present in the
array.

 11 12 25 22 64

 While traversing, 22 came out to be the third least value and


it should appear at the third place in the array, thus swap 22
with element present at third position.

 11 12 22 25 64
 Fourth pass:

 Similarly, for fourth position traverse the rest of the array and find the
fourth least element in the array
 As 25 is the 4th lowest value hence, it will place at the fourth position.

 11 12 22 25 64

 Fifth Pass:

 At last the largest value present in the array automatically get placed at
the last position in the array
 The resulted array is the sorted array.

 11 12 22 25 64

You might also like