0% found this document useful (0 votes)
13K views

String Handling Library Functions

This document summarizes some string handling functions in C including string conversion functions like atof(), atoi(), atol() and strtol() that convert strings to numeric types. It also discusses character arithmetic, increment, decrement and addition/subtraction on character ASCII values. Finally, it provides an example of sorting strings using strcmp() and strcpy() functions to compare and copy strings.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13K views

String Handling Library Functions

This document summarizes some string handling functions in C including string conversion functions like atof(), atoi(), atol() and strtol() that convert strings to numeric types. It also discusses character arithmetic, increment, decrement and addition/subtraction on character ASCII values. Finally, it provides an example of sorting strings using strcmp() and strcpy() functions to compare and copy strings.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

• Some other functions from string handling

library
Outline
• String Conversion Functions
• Character arithmetic
• Sorting of strings.
String Conversion Functions
• String Conversion functions
– In <stdlib.h> (general utilities library)
• Convert strings of digits to integer and
floating-point values
Function prototype Function description
double atof( const char *nPtr ); Converts the string nPtr to double.
int atoi( const char *nPtr ); Converts the string nPtr to int.
long atol( const char *nPtr ); Converts the string nPtr to long int.
double strtod( const char *nPtr, char Converts the string nPtr to double.
**endPtr );
long strtol( const char *nPtr, char Converts the string nPtr to long.
**endPtr, int base );
unsigned long strtoul( const char Converts the string nPtr to unsigned long.
*nPtr, char **endPtr, int base );
atof()
Function atof
• Function atof converts its argument—a string
that represents a floating-point number—to
a double value.
• The function returns the double value.
• If the converted value cannot be represented
—for example, if the first character of the
string is a letter—the behavior of function atof
is undefined.
Example Code
This program
demonstrates
string
conversion
function: atof()

The string "99.0" converted to double is 99.000


The converted value divided by 2 is 49.500 output
atoi()
Function atoi
• Function atoi converts its argument—a string
of digits that represents an integer— to an int
value.
• The function returns the int value.
• If the converted value cannot be represented,
the behavior of function atoi is undefined.
Example Code
This program
demonstrates
string
conversion
function: atoi()

The string "2593" converted to int is 2593


The converted value minus 593 is 2000 output
atol()
Function atol
• Function atol converts its argument—a string of
digits representing a long integer— to a long
value.
• The function returns the long value.
• If the converted value cannot be represented,
the behavior of function atol is undefined.
• If int and long are both stored in 4 bytes, function
atoi and function atol work identically.
Example Code
This program
demonstrates
string
conversion
function: atol()

The string "1000000" converted to long int is 1000000


The converted value divided by 2 is 500000 output
strtol()
• Function strtol converts to long a sequence of
characters representing an integer.
• The function receives three arguments—a string
(char *), a pointer to a string and an integer.
• The string contains the character sequence to be
converted.
• The pointer is assigned the location of the first
character after the converted portion of the string.
• The integer specifies the base of the value being
converted(octal, decimal or hexadecimal format).
This program
demonstrates
string
conversion
function: strtol()

The original string is "-1234567abc"


The converted value is -1234567
The remainder of the original string is "abc"
The converted value plus 567 is -1234000
strstr
• Function strstr <string.h>
char *strstr(const char *s1, const char *s2);
• Function strstr searches for the first occurrence
of its second string argument in its first string
argument.
• If the second string is found in the first string, a
pointer to the location of the string in the first
string argument is returned.
• Otherwise, a NULL pointer is returned
#include <stdio.h>
#include <string.h> Program
int main( void ) searches for the
{
const char *string1 = "abcdefabcdef"; /* string to
first occurrence
search */
const char *string2 = "def"; /* string to search for
of string2 in
*/ string1.
printf( "%s%s \n%s%s\ n\n %s\ n %s%s\n",
"string1 = ", string1, "string2 = ", string2, "The
remainder of string1 beginning with the", "first
occurrence of string2 is:", strstr(string1, string2));

} /* end main */

string1 = abcdefabcdef
string2 = def

The remainder of string1 beginning with the


first occurrence of string2 is: defabcdef
Character arithmetic
• To perform increment , decrement, addition
subtraction operations on the characters.
• These operations work on the ASCII value of
characters.
• Starting from ASCII value of ‘a’ = 97 to the
ASCII value of ‘z’ = 122
Increment
• To display next char value
void main()
{
char x = 'a' + 1;
printf("%c", x); // Display Result = 'b‘
printf("%c", ++x); // Display Result = ‘c‘

}
Decrement
• To display previous char value
void main()
{
char x = ‘b' - 1;
printf("%c", x); // Display Result = ‘a‘
}
Addition
• Adding two ASCII values
void main()
{
char x = 'a‘ + ‘c’;
printf("%c", x); /* Display Result = - ( addition of
ASCII of a and c is 196) */
}
Subtraction
• Adding two ASCII values
void main()
{
char x = ‘z’ – ‘a’;
printf("%c",x); /* Display Result = ↓ (difference
between ASCII of z and a ) */
}
Sorting of strings
• To sort the strings in increasing order.
– That is if list of names is given then sort the list in
alphabetical order.
• Use strcmp() and strcpy() functions.
#include<stdio.h>
#include<conio.h>
#include<string.h>
Program to sort
main()
{ the strings using
char name[20][20];
char temp[20];
int i,j,n,l;
arrays.
printf("Enter the no. of string to be sorted");
scanf("%d",&n);
printf(“Enter %d strings:\n", n);
for(i=0;i<=n;i++)
gets(name[i]);
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
l = strcmp(name[i], name[j]);
if(l>0) // if first string is greater then swap
{
strcpy(temp, name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
puts("Sorted list is:\n");
for(i=0;i<=n;i++)
puts(name[i]);
getch();
}
Enter the no. of string to be sorted 3
Enter 3 strings
lovely
aananya
aman preet
Sorted list is:
aananya
aman preet
lovely

You might also like