Characters and Strings

Similar documents
C: How to Program. Week /May/28

Chapter 8 - Characters and Strings

Chapter 8 C Characters and Strings

by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming. Lecture 11: C Characters and Strings

C Characters and Strings

Chapter 8: Character & String. In this chapter, you ll learn about;

Contents. Preface. Introduction. Introduction to C Programming

Scientific Programming in C V. Strings

Computer Programming

Structured programming

Outline. Computer Programming. Structure of a function. Functions. Function prototype. Structure of a function. Functions

C mini reference. 5 Binary numbers 12

C Libraries. Bart Childs Complementary to the text(s)

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

C PROGRAMMING. Characters and Strings File Processing Exercise

Lecture 05 Pointers ctd..

8. Characters, Strings and Files

Appendices E through H are PDF documents posted online at the book s Companion Website (located at

today cs3157-fall2002-sklar-lect05 1

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

N v 1. Type generic string interfaces honor the const contract of application code ISO/IEC JTC 1/SC 22/WG14. August 20, 2016

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Introduction to string

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Lecture 10 Arrays (2) and Strings. UniMAP SEM II - 11/12 DKT121 1

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Create a Program in C (Last Class)

Computer Programming: Skills & Concepts (CP) Strings

C Programming. Unit 9. Manipulating Strings File Processing.

CS167 Programming Assignment 1: Shell

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

Pointers, Arrays, and Strings. CS449 Spring 2016

Fundamentals of Programming & Procedural Programming

Strings and Library Functions

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Chapter 9 Strings. With this array declaration: char s[10];

Arrays, Strings, & Pointers

ONE DIMENSIONAL ARRAYS

Library and function of C. Dr. Donald Davendra Ph.D. (Department of ComputingLibrary Science, andfei function VSB-TU of COstrava)

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

,$5(0%(''(':25.%(1&+ $16,&'(9(/230(17722/6 EMBEDDED WORKBENCH ANSI C COMPILER C-SPY FOR NATIONAL SEMICONDUCTOR CORP. S &RPSDFW5,6& 70 &5

Course organization. Course introduction ( Week 1)

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Procedural Programming

System Design and Programming II

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

C Programming Language Review and Dissection III

1 Pointer Concepts. 1.1 Pointer Examples

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

LECTURE 15. String I/O and cstring library

SWEN-250 Personal SE. Introduction to C

Computers Programming Course 11. Iulian Năstac

ECE551 Midterm Version 1

ECE551 Midterm Version 2

Chapter 8 Character Arrays and Strings

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Strings. Chuan-Ming Liu. Computer Science & Information Engineering National Taipei University of Technology Taiwan

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

MC8051: Speichertypen und Adressräume

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

Intermediate Programming, Spring 2017*

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Strings. Daily Puzzle

Approximately a Test II CPSC 206

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

CROSSWARE C8051NT ANSI C Compiler for Windows

ECE551 Midterm Version 1

LAB7 : Characters and Strings

CS107 Spring 2019, Lecture 4 C Strings

Strings. Steven R. Bagley

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

C programming basics T3-1 -

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Characters, Character Strings, and string-manipulation functions in C

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Introduction C CC. Advanced C

Index. backslash character, 19 backup, off-site, 11. abs, 72 abstraction, 63, 83, 133, 141, 174, 181 acos, 72

LESSON 4. The DATA TYPE char

Arrays and Strings (2H) Young Won Lim 3/7/18

Utility Functions. 3.1 Introduction

CS3157: Advanced Programming. Outline

CSC 270 Survey of Programming Languages. C-String Values

CS107, Lecture 4 C Strings

AVR Development Tools

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Transcription:

Characters and Strings 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay

Character constants A character in single quotes, for example, a It value is determined by the underlying character set, ASCII, for example Thus the value of z and \n in the ASCII character set is 122 and 10 respectively

String constants A string constant is a sequence of characters made up of alphanumeric characters, special characters such as +, -, *, / and $ in double quotes Examples: John Doe 99999 Main Street

String data structure In C a string is represented as an array of characters For example: char name[] = asish or char name[] = { a, s, i, s, h, \0 }

Character-handling library <ctype.h> isdigit Syntax: int isdigit( int c ) Explanation: Returns a true value if c is a digit and 0 ( false ) otherwise Almost identical syntax for: isalpha, isalnum, isxdigit See Fig. 8.2 of your text for a sample use.

Character-handling library <ctype.h> islower Syntax: int islower( int c ) Explanation: Returns a true value if c is a lowercase letter and 0 otherwise Almost Identical syntax for: isupper See Fig. 8.2 of your text for a sample use

Character-handling library <ctype.h> tolower Syntax: int tolower( int c) Explanation: Returns a lowercase letter if c is an uppercase letter; otherwise returns the argument unchanged Identical syntax for: toupper Sample use: See Fig. 8.3 of your text

Character-handling library <ctype.h> isspace Syntax : int isspace (int c) Explanation: Returns a true value if c is a whitespace character and 0 otherwise Identical syntax for: iscntrl, ispunct, isprint, isgraph See Fig. 8.4 of text for sample use

String conversion functions <stdlib.h> strtod Syntax: double strtod( const char *nptr, char ** endptr ) Explanation: Converts the string nptr to double Sample Use:

strtod #include <stdio.h> #include <stdlib.h> int main( void ) { // initialize string pointer const char *string = "51.2% are admitted"; // initialize string double d; // variable to hold converted sequence char *stringptr; // create char pointer d = strtod( string, &stringptr ); printf( "The string \"%s\" is converted to the\n", string ); printf( "double value %.2f and the string \"%s\"\n", d, stringptr ); } // end main

strtod Output: The string 51.2% are admitted is converted to the double value 51.20 and the string % are admitted

String conversion functions strtol Syntax: long strtol( const char *nptr, char ** endptr, int base ) Explanation: Converts the string nptr to long Sample Use:

#include <stdio.h> #include <stdlib.h> strtol int main( void ) { const char *string = "-1234567abc"; // initialize string pointer char *remainderptr; // create char pointer long x; // variable to hold converted sequence x = strtol( string, &remainderptr, 0 ); printf( "%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n", "The original string is ", string, "The converted value is ", x, "The remainder of the original string is ", remainderptr, "The converted value plus 567 is ", x + 567 ); } // end main

strtol Output: 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

String conversion functions strtoul : Reading assignment

Standard I/O library functions fgets and putchar Syntax: char *fgets(char *s, int n, FILE *stream) Explanation: input characters from the specified stream into the array s until a newline or end-offile character is encountered or until n-1 bytes are read. A terminating null character is appended to the array. Returns the string that was read into s

Standard I/O library functions Syntax: int putchar(int c) Explanation: Prints the character stored in c and returns it as an integer Sample use

fgets and putchar #include <stdio.h> #define SIZE 80 void reverse( const char * const sptr ); // prototype int main( void ) { char sentence[ SIZE ]; // create char array puts( "Enter a line of text:" ); // use fgets to read line of text fgets( sentence, SIZE, stdin ); puts( "\nthe line printed backward is:" ); reverse( sentence ); } // end main

fgets and putchar // recursively outputs characters in string in reverse order void reverse( const char * const sptr ) { // if end of the string if ( '\0' == sptr[ 0 ] ) { // base case return; } // end if else { // if not end of the string reverse( &sptr[ 1 ] ); // recursion step putchar( sptr[ 0 ] ); // use putchar to display character } // end else } // end function reverse Output: Enter a line of text: Characters and Strings The line printed backward is: sgnirts dna sretcarahc

Standard I/O library functions getchar Syntax: int getchar( void) Explanation: Inputs the next character from the standard input and returns it as an integer Sample use

getchar #include <stdio.h> #define SIZE 80 int main( void ) { int c; // variable to hold character input by user char sentence[ SIZE ]; // create char array int i = 0; // initialize counter i // prompt user to enter line of text puts( "Enter a line of text:" );

getchar // use getchar to read each character while ( i < SIZE - 1 && ( c = getchar() )!= '\n' ) { sentence[ i++ ] = c; } // end while sentence[ i ] = '\0'; // terminate string // use puts to display sentence puts( "\nthe line entered was:" ); puts( sentence ); } // end main Output: Enter a line of text: This is a test The line entered was: This is a test

Standard I/O library functions sprintf Syntax: int sprintf( char *s, const char *format,.) Explanation: Equivalent to printf, except that the output is stored in the array s instead of printed on the screen. Returns the number of characters written to s or EOF if an error occurs. Sample use

sprintf #include <stdio.h> #define SIZE 80 int main( void ) { char s[ SIZE ]; // create char array int x; // x value to be input double y; // y value to be input puts( "Enter an integer and a double:" ); scanf( "%d%lf", &x, &y ); sprintf( s, "integer:%6d\ndouble:%8.2f", x, y ); printf( "%s\n%s\n", "The formatted output stored in array s is:", s ); } // end main

sprintf Output: Enter an integer and a double: 298 87.375 The formatted output stored in array s is: 298 87.38

Standard I/O library functions sscanf Syntax: int sscanf( char *s, const char *format,.) Explanation: Equivalent to sprintf, except that input is read from the array s rather than from the keyboard. Returns the number of items successfully read by the function, or EOF if an error occurs Sample use

sscanf #include <stdio.h> #define SIZE 80 int main( void ) { char s[ SIZE ]; // create char array int x; // x value to be input double y; // y value to be input puts( "Enter an integer and a double:" ); scanf( "%d%lf", &x, &y ); sprintf( s, "integer:%6d\ndouble:%8.2f", x, y ); printf( "%s\n%s\n", "The formatted output stored in array s is:", s ); } // end main Output: The values stored in the character array s are: integer: 31298 double: 87.375

String manipulation functions strcpy Syntax: int strcpy(char *s1, const char *s2); Explanation: Copies the string s2 into array s1; the value of s1 is returned Sample use:

String manipulation functions Strncpy Syntax: int strncpy(char *s1, const char *s2); Explanation: Copies at most n characters of the string s2 into array s1; the value of s1 is returned Sample use:

strcpy and strncpy #include <stdio.h> #include <string.h> #define SIZE1 25 #define SIZE2 15 int main( void ) { char x[] = "Happy Birthday to You"; // initialize char array x char y[ SIZE1 ]; // create char array y char z[ SIZE2 ]; // create char array z // copy contents of x into y printf( "%s%s\n%s%s\n", "The string in array x is: ", x, "The string in array y is: ", strcpy( y, x ) );

strcpy and strncpy // copy first 14 characters of x into z. Does not copy null // character strncpy( z, x, SIZE2-1 ); z[ SIZE2-1 ] = '\0'; // terminate string in z printf( "The string in array z is: %s\n", z ); } // end main Output: The string in array x is: Happy birthday to you The string in array y is: Happy birthday to you The string in array z is: Happy birthday

String manipulation functions strcat Syntax: char *strcat(char *s1, const char *s2); Explanation: Appends string s2 to array s1; the first character of s2 overwrites the terminating null character of s1. The value of s1 is returned Sample use

String manipulation functions strncat Syntax: char *strncat(char *s1, const char *s2); Explanation: Appends at most n characters of the string s2 to array s1; the first character of s2 overwrites the terminating null character of s1. The value of s1 is returned Sample use

strcat and strncat #include <stdio.h> #include <string.h> int main( void ) { char s1[ 20 ] = "Happy "; // initialize char array s1 char s2[] = "New Year "; // initialize char array s2 char s3[ 40 ] = ""; // initialize char array s3 to empty printf( "s1 = %s\ns2 = %s\n", s1, s2 ); // concatenate s2 to s1 printf( "strcat( s1, s2 ) = %s\n", strcat( s1, s2 ) );

strcat and strncat // concatenate first 6 characters of s1 to s3. Place '\0' // after last character printf( "strncat( s3, s1, 6 ) = %s\n", strncat( s3, s1, 6 ) ); // concatenate s1 to s3 printf( "strcat( s3, s1 ) = %s\n", strcat( s3, s1 ) ); } // end main Output: s1= Happy s2 = New Year strrcat(s1, s2) = Happy New Year strncat(s3, s1, 6) = Happy strcat (s3, s1) = Happy Happy New Year

Comparison functions strcmp Syntax: int strcmp(const char *s1, const char *s2); Explanation: Compares the string s1 with the string s2; returns 0, > 0, < 0 respectively if s1 is equal to, greater than or less than s2 Sample use:

Comparison functions strncmp Syntax: int strncmp(const char *s1, const char *s2); Explanation: Compares up to n characters of the string s1 with the string s2; returns 0, > 0, < 0 respectively if s1 is equal to, greater than or less than s2 Sample use:

#include <stdio.h> #include <string.h> strcmp and strncmp int main( void ) { const char *s1 = "Happy New Year"; // initialize char pointer const char *s2 = "Happy New Year"; // initialize char pointer const char *s3 = "Happy Holidays"; // initialize char pointer printf("%s%s\n%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n\n", "s1 = ", s1, "s2 = ", s2, "s3 = ", s3, "strcmp(s1, s2) = ", strcmp( s1, s2 ), "strcmp(s1, s3) = ", strcmp( s1, s3 ), "strcmp(s3, s1) = ", strcmp( s3, s1 ) );

strcmp and strncmp printf("%s%2d\n%s%2d\n%s%2d\n", "strncmp(s1, s3, 6) = ", strncmp( s1, s3, 6 ), "strncmp(s1, s3, 7) = ", strncmp( s1, s3, 7 ), "strncmp(s3, s1, 7) = ", strncmp( s3, s1, 7 ) ); } // end main Output: s1 = Happy New Year s2 = Happy New Year s3 = Happy Holidays strcmp(s1, s2) = 0 strcmp(s1, s3) = 1 strcmp(s3, s21= -1 strncmp(s1, s2,6) = 0 strncmp(s1, s3,7) = 6 strncmp(s3, s1, 7)= -6

Search functions strchr Syntax: void *strchr(const char *s, int c) Explanation: Locates the first occurrence of character c in string s. If found, a pointer to c in s is returned. Otherwise, a NULL pointer is returned Sample use:

strchr #include <stdio.h> #include <string.h> int main( void ) { const char *string = "This is a test"; // initialize char pointer char character1 = 'a'; // initialize character1 char character2 = 'z'; // initialize character2 // if character1 was found in string if ( strchr( string, character1 )!= NULL ) { printf( "\'%c\' was found in \"%s\".\n", character1, string ); } // end if else { // if character1 was not found printf( "\'%c\' was not found in \"%s\".\n", character1, string ); } // end else

strchr // if character2 was found in string if ( strchr( string, character2 )!= NULL ) { printf( "\'%c\' was found in \"%s\".\n", character2, string ); } // end if else { // if character2 was not found printf( "\'%c\' was not found in \"%s\".\n", character2, string ); } // end else } // end main Output: a was found in This is a test. z was not found in This is a test.

Search functions strcspn Syntax: void *strcspn(const char *s1, const char *s2) Explanation: Determines and returns the length of the initial segment of s1 consisting of characters not contained in s2. Sample use:

strcspn This program is missing How about writing one form scratch?

#include <stdio.h> #include <string.h> strcspn int main(void) { const char *string1 = "The value is 3.14159"; // no const to avoid warning const char *string2 = "1234567890"; int length = 0; char *p = string1; } while (*p!= '\0'){ if(strchr(string2, *p) == NULL) ++length; else break; p++; } // end while printf("the initial part of the string is of length %d\n", length);

Search functions strpbrk Syntax: void *strpbrk(const char *s1, const char *s2) Explanation: Locates the first occurrence in string s1 of any character in s2; if found, a pointer to the character in s1 is returned, else NULL is returned. Sample use:

strpbrk #include <stdio.h> #include <string.h> int main( void ) { const char *string1 = "This is a test"; // initialize char pointer const char *string2 = "beware"; // initialize char pointer printf( "%s\"%s\"\n'%c'%s\n\"%s\"\n", "Of the characters in ", string2, *strpbrk( string1, string2 ), " appears earliest in ", string1 ); } // end main Output: Of the characters in beware a appears earliest in This is a test

Search functions strrchr Syntax: void *strrchr(const char *s, int c) Explanation: Locates the last occurrence of c in string s ; if found, a pointer to the character in s is returned, else NULL is returned. Sample use:

strrchr #include <stdio.h> #include <string.h> int main( void ) { // initialize char pointer const char *string1 = "A zoo has many animals including zebras"; int c = 'z'; // character to search for printf( "%s\n%s'%c'%s\"%s\"\n", "The remainder of string1 beginning with the", "last occurrence of character ", c, " is: ", strrchr( string1, c ) ); } // end main Output: The remainder of the string with the beginning with the last occurrence of character z is : zebras

Search functions strspn Syntax: void *strcspn(const char *s1, const char *s2) Explanation: Determines and returns the length of the initial segment of s1 consisting only of characters contained in s2. Sample use

#include <stdio.h> #include <string.h> strspn int main( void ) { // initialize two char pointers const char *string1 = "The value is 3.14159"; const char *string2 = "aehi lstuv"; printf( "%s%s\n%s%s\n\n%s\n%s%u\n", "string1 = ", string1, "string2 = ", string2, "The length of the initial segment of string1", "containing only characters from string2 = ", strspn( string1, string2 ) ); } // end main Output: string1 = The value is 3.14159 string2 = aehi lstuv The length of the initial segment of string1 containing only characters fro string2 is 13

Search functions strstr Syntax: char *strstr(const char *s1, const char* s2) Explanation: Locates the fisrt occurrence of syring s2 in string s1 ; if found, a pointer to the occurrence in s1 is returned, else NULL is returned. Sample use

strstr #include <stdio.h> #include <string.h> int main( void ) { const char *string1 = "abcdefabcdef"; // string to search const char *string2 = "def"; // string to search for 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 Output: string1 = abcdefabcdef string2 = def The remainder of the string1 beginning with the first occurrence of string 2 is defabcedf

Search functions strtok Syntax: char *strtok(const char *s1, const char* s2) Explanation: A sequence of calls to strtok breaks string s1 into tokens logical pieces separated by characters in string s2. The first call has s1 as the first argument; subsequent calls NULL, tokenizing the same string. A pointer to the current token is returned by each call. NULL is returned when there are no more tokens left. Sample use

strtok #include <stdio.h> #include <string.h> int main( void ) { // initialize array string char string[] = "This is a sentence with 7 tokens"; char *tokenptr; // create char pointer printf( "%s\n%s\n\n%s\n", "The string to be tokenized is:", string, "The tokens are:" ); tokenptr = strtok( string, " " ); // begin tokenizing sentence

strtok // continue tokenizing sentence until tokenptr becomes NULL while ( tokenptr!= NULL ) { printf( "%s\n", tokenptr ); tokenptr = strtok( NULL, " " ); // get next token } // end while } // end main Output: The string to be tokensized is: This is a sentence with 7 tokens The tokens are: This Is a sentence with 7 tokens

Memory functions The string handling functions of this genre manipulate, compare and search blocks of memory

Memory functions memcpy Syntax: void *memcpy(void *s1, const void *s2, size_t n) Explanation: Copies n characters from the object pointed to by s2 into the object pointed to by s1. A pointer to the resulting object is returned Sample use:

memcpy #include <stdio.h> #include <string.h> int main( void ) { char s1[ 17 ]; // create char array s1 char s2[] = "Copy this string"; // initialize char array s2 memcpy( s1, s2, 17 ); printf( "%s\n%s\"%s\"\n", "After s2 is copied into s1 with memcpy,", "s1 contains ", s1 ); } // end main

Memory functions memmove Syntax: void *memmove(void *s1, const void *s2, size_t n) Explanation: Copies n characters from the object pointed to by s2 into the object pointed to by s1 by first copying into a temporary array. Returns pointer to resulting object Sample use

memmove #include <stdio.h> #include <string.h> int main( void ) { char x[] = "Home Sweet Home"; // initialize char array x printf( "%s%s\n", "The string in array x before memmove is: ", x ); printf( "%s%s\n", "The string in array x after memmove is: ", (char *) memmove( x, &x[ 5 ], 10 ) ); } // end main Output: The string in the array x before memmove is: Home Sweet Home The string in the array x after memmove is: Sweet Home Home

Memory functions memcmp Syntax: void *memcmp(void *s1, const void *s2, size_t n) Explanation: Compares the first n characters of the objects pointed to by s1 and s2; returns 0, less than 0 or greater than 0 if s1 is equal to, less than or greater than s2. Sample use

memcmp #include <stdio.h> #include <string.h> int main( void ) { char s1[] = "ABCDEFG"; // initialize char array s1 char s2[] = "ABCDXYZ"; // initialize char array s2 printf( "%s%s\n%s%s\n\n%s%2d\n%s%2d\n%s%2d\n", "s1 = ", s1, "s2 = ", s2, "memcmp( s1, s2, 4 ) = ", memcmp( s1, s2, 4 ), "memcmp( s1, s2, 7 ) = ", memcmp( s1, s2, 7 ), "memcmp( s2, s1, 7 ) = ", memcmp( s2, s1, 7 ) ); } // end main Output: s1 = ABCDEFG s2 = ABCDXYZ memcmp(s1, s2, 4) =0 memcmp(s1, s2, 7) = -1 memcmp(s2, s1, 7) = 1

Memory functions memchr Syntax: void *memcmp( const void *s, int c, size_t n) Explanation: Locates the first occurrence of c in the first n characters of the object pointed to by s; if found a pointer to c in the object is returned else NULL is returned Sample use

memchr #include <stdio.h> #include <string.h> int main( void ) { const char *s = "This is a string"; // initialize char pointer printf( "%s\'%c\'%s\"%s\"\n", "The remainder of s after character ", 'r', " is found is ", (char *) memchr( s, 'r', 16 ) ); } // end main Output: The remainder of s after character r is found is ring

Memory functions memset Syntax: void *memset( void *s, int c, size_t n) Explanation: Copies c into the first n characters of the object pointed to by s; a pointer to the result is returned Sample use

memset #include <stdio.h> #include <string.h> int main( void ) { char string1[ 15 ] = "BBBBBBBBBBBBBB"; // initialize string1 printf( "string1 = %s\n", string1 ); printf( "string1 after memset = %s\n", (char *) memset( string1, 'b', 7 ) ); } // end main Output: string1 = BBBBBBBBBBBBBB string 1 after memset =bbbbbbbbbbbbbb

Other functions strerror Syntax: char *strerror(int ernum) Explanation: Maps errnum into a full text string in a locale-compiler manner. A pointer to the string is returned Sample use:

strerror #include <stdio.h> #include <string.h> int main( void ) { printf( "%s\n", strerror( 2 ) ); } // end main Output: No such file or directory

Other functions strlen Syntax: size_t strlen(const char *s) Sample use :

strlen #include <stdio.h> #include <string.h> int main( void ) { // initialize char pointer const char *string = "abcdefghi"; printf("%s\"%s\"%s%u\n", "The length of ", string, " is ", strlen( string1 )) } // end main Output: The length of abcdefghi is 9