0% found this document useful (0 votes)
38 views15 pages

Computer Programming: Dr. Osman Parlaktuna Dr. Burak Kaleci

This document discusses various string manipulation and input/output functions from the C standard library. It describes functions like getchar(), putchar(), sprintf(), sscanf() for standard input/output. It also covers string handling functions such as strcpy(), strncpy(), strcat(), strncat() for copying and concatenating strings, and strcmp(), strncmp() for comparing strings. The document is presented as part of a lecture on computer programming by Dr. Osman Parlaktuna and Dr. Burak Kaleci.

Uploaded by

mivri
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)
38 views15 pages

Computer Programming: Dr. Osman Parlaktuna Dr. Burak Kaleci

This document discusses various string manipulation and input/output functions from the C standard library. It describes functions like getchar(), putchar(), sprintf(), sscanf() for standard input/output. It also covers string handling functions such as strcpy(), strncpy(), strcat(), strncat() for copying and concatenating strings, and strcmp(), strncmp() for comparing strings. The document is presented as part of a lecture on computer programming by Dr. Osman Parlaktuna and Dr. Burak Kaleci.

Uploaded by

mivri
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/ 15

Standard Input/Output Library Functions

String-Manipulation Functions of the String-Handling Library


Comparison Functions of the String-Handling Library

Computer Programming

Dr. Osman Parlaktuna


Dr. Burak Kaleci

March 11, 2017

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

Content

Standard Input/Output Library Functions

String-Manipulation Functions of the String-Handling Library

Comparison Functions of the String-Handling Library

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

Introduction
Standard Input/Output library functions could be used to
manipulate characters and strings.
I int getchar (void) Inputs the next character from the standard
input, in other words, keyboard, and returns it as an integer.
I int putchar (int c) Prints the character stored in c and returns
it as an integer.
I int sprintf( char* s, const char* format, ... ) Equivalent to
printf, except the output is stored in the array s instead of
printed on the screen. Returns the number of characters
written to s.
I int sscanf( char* s, const char* format, ... ) Equivalent to
scanf, except the input is read from the array s rather than
from the keyboard. Returns the number of items successfully
read by the function.
Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming
Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

getchar and putchar

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

sprintf
sprintf prints formatted data into a char array. The function uses
the same conversion specifiers as printf.

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

sscanf

sscanf reads formatted data from character array. The function


uses the same conversion specifiers as scanf.

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

Introduction

The string-handling library (<string.h>) provides many useful


functions
I manipulating string data (copying strings and concatenating
strings)
I comparing strings
I searching strings for characters and other strings
I tokenizing strings (separating strings into logical pieces)
I determining the length of strings.

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

Manipulating string data

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

strcpy and strncpy


I strcpy copies its second argument (a string) into its first
argument,a character array,
I The character array must be large enough to store the string
and its terminating null character, which is also copied.
I strncpy is equivalent to strcpy, except that strncpy specifies
the number of characters to be copied from the string into the
array.
I strncpy does not necessarily copy the terminating null
character of its second argument.
I This occurs only if the number of characters to be copied is at
least one more than the length of the string.
I Assume that test is the second argument and a terminating
null character is written only if the third argument to strncpy
is at least 5(four characters in testplus a terminating null
character).
Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming
Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

strcpy and strncpy

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

strcat and strncat

I strcat appends its second argument (a string) to its first


argument (a character array containing a string).
I You must ensure that the array used to store the first string is
large enough to store the first string, the second string and
the terminating null character copied from the second string.
I strncat appends a specified number of characters from the
second string to the first string. A terminating null character
is automatically appended to the result.

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

strcpy and strncpy

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming


Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

strcmp and strncmp

I strcmp compares its first string argument with its second


string argument, character by character.
I strncmp is equivalent to strcmp, except that strncmp
compares up to a specified number of characters.
I strncmp does not compare characters following a null
character in a string.
Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming
Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

strcmp and strncmp


I strcmp returns 0 if the strings are equal, a negative value if
the first string is less than the second string and a positive
value if the first string is greater than the second string.
I what does one string to be greater than or less than another
means?
I We know that Jones places before Smith, because the first
letter of Jones comes before the first letter of Smith in the
alphabet.
I How do the string comparison functions know that one
particular letter comes before another?
I All characters are represented inside the computer as numeric
codes in character sets such as ASCII and Unicode; when the
computer compares two strings, it actually compares the
numeric codes of the characters in the strings.
Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming
Standard Input/Output Library Functions
String-Manipulation Functions of the String-Handling Library
Comparison Functions of the String-Handling Library

strcmp and strncmp

Dr. Osman Parlaktuna Dr. Burak Kaleci Computer Programming

You might also like