Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to C Programming
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Pseudocode A way to make programming easier Start with a verbal description of what the program is supposed to do! Slowly transform it into C, defining.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
C-strings Array with base type char One character per indexed variable
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Strings in C are my friend, and they can be yours, too. An inspiring and epic tale by Erik Speyer, John Sullivan, and Dane Bennington.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Fundamentals of Characters and Strings
CSE 303 Lecture 14 Strings in C
Pointers and Pointer-Based Strings
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
Characters and Strings Functions
Introduction to Problem Solving and Programming
Presentation transcript:

Strings string.h library

String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy copies string from source to dest strncopy copies n chars from source to dest strcat appends string from source to end of dest strncat appends n chars from source to end of dest strcmp compares two strings alphabetically strncmp compares the first n chars of two strings strstr finds a string inside another strtok breaks string into tokens using delimiters #include

strlen strlen(char *s) – Input: Pointer to a string array – Output: Returns the number of chars in array until the null character – Example: intN; char s1[15] = “Hello”; char s2[15] = “My name is:”; N = strlen(s1);/* N is 5 */ N = strlen(s2);/* N is 11 */ N = strlen(“How are you?”); /* N is 12 */ Dr. Sadık EşmelioğluCENG 1143

strcpy strcpy(char *dest, char *src) – Input: Pointers to destination and source char arrays – Output: Copies src string into dest string – Example: char s1[15] = “Hello”; char s2[15] = “My name is:”; chars3[40]; strcpy(s3, s1);/* s3 contains Hello\0 */ strcpy(s3, s2);/* s3 contains My Name is:\0 */ strcpy(s3, “How are you?”); /* s3 contains How are you?\0 */ Dr. Sadık EşmelioğluCENG 1144

strncpy strncpy(char *dest, char *src, int n) – Input: Pointers to dest and src char arrays, and an int – Output: Copies the first n chars from src into dest string; if the length of src is less than n, remaining space is filled with null char – Example: char s1[15] = “Hello”; char s2[15] = “My name is:”; chars3[40]; strncpy(s3, s1, 7);/* s3 contains Hello\0\0 */ strncpy(s3, s2, 7);/* s3 contains My Name */ strncpy(s3, “How are you?”, 7); /* s3 contains How are */ Dr. Sadık EşmelioğluCENG 1145

strcat strcat(char *dest, char *src) – Input: Pointers to destination and source char arrays – Output: Appends src string into dest string including the null pointer – Example: char s1[15] = “Hello”; char s2[6] = “John”; chars3[40]; strcpy(s3, s1);/* s3 contains Hello\0 */ strcat(s3, “ “);/* s3 contains Hello \0*/ strcat(s3, s2); /* s3 contains Hello John\0*/ Dr. Sadık EşmelioğluCENG 1146

strncat strncat(char *dest, char *src, int n) – Input: Pointers to dest and src char arrays, and an integer – Output: Appends the first n chars of the src string to the end of dest string; if the length of src is less than n, only the src is copied with a null char at the end, if more, the first n chars and the null char are appended. – Example: char s1[15] = “Hello”; char s2[6] = “John”; chars3[40]; strcpy(s3, s1);/* s3 contains Hello\0 */ strcat(s3, “ “);/* s3 contains Hello \0*/ strncat(s3, s2, 6); /* s3 contains Hello John\0\0*/ strncat(s3, s1, 3);/* s3 contains Hello JohnHel\0*/ Dr. Sadık EşmelioğluCENG 1147

strcmp strcmp(char *s 1, char *s 2 ) – Input: Pointers to two strings – Output: compares s 1 to s 2 alphabetically and returns 0 if identical, a negative value if s1 comes before s2, a positive value if s2 comes before s1 – Example: intn; char s1[15] = “Can”; char s2[15] = “Caner”; chars3[15] = “Cem”; n = strcmp(s1, “Can”);/* n is zero*/ n = strcmp(s1, s2);/* n is negative */ n = strcmp(s2, s1);/* n is positive*/ n = strcmp(s1, s3);/* n is negative*/ Dr. Sadık EşmelioğluCENG 1148

strncmp strncmp(char *s 1, char *s 2, int n ) – Input: Pointers to two strings and an integer – Output: compares the first n chars of s 1 to s 2 alphabetically and returns 0 if identical, a negative value if s1 comes before s2, a positive value if s2 comes before s1 – Example: intn; char s1[15] = “Can”; char s2[15] = “Caner”; chars3[15] = “Cem”; n = strncmp(s1, s2, 4);/* n is negative */ n = strncmp(s1, s2, 3);/* n is zero*/ n = strncmp(s1, s3, 3);/* n is negative*/ Dr. Sadık EşmelioğluCENG 1149

strstr strstr(char *s 1, char *s 2 ) – Input: Pointers to two strings – Output: searches for s2 in s1 and returns a pointer to the starting location of s2 in s1, NULL pointer if s2 cannot be found in s1 – Example: char str1[25] = "I cannot stay long"; char str2[25]; printf("Enter a string: "); scanf("%s", str2); if(strstr(str1, str2) == NULL) printf("The string entered is not in the sentence!\n"); else printf("The string entered is in the sentence!\n"); Dr. Sadık EşmelioğluCENG 11410

strstr – Example 2: char str1[25] = "I cannot stay long"; char str2[25]; char *ret; printf("Please enter a four-letter verb: "); scanf("%s", str2); ret = strstr(str1, "stay"); strncpy(ret, str2, 4); puts(str1); Dr. Sadık EşmelioğluCENG 11411

strtok strtok(char *s 1, char *delim) – Input: Pointers to two strings. s1 is the string to be separated into tokens, and delim is a list of separators. – Output: Marks the first non-delim character as the beginning of a token, continues until it reaches a delim character, replaces that character with \ 0 marking the end of the token. Subsequent calls to this function with NULL as the first argument, continues to operate on the same string from the point first call left off. Returns a pointer to the beginning of the token. For subsequent calls with NULL as the first argument, if no delimiter is found, returns NULL. Dr. Sadık EşmelioğluCENG Example: char s1[] = “Can-Tan-Betul”; strtok(s1, “-”); strtok(NULL, “-”); Can-Tan-Betul\0 Can Tan-Betul Can Tan Betul Can Tan Betul

strtok Example: char str[] = "Hearts#Spades#Diamonds#Clubs"; char delims[] = "#"; char *result; result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } Dr. Sadık EşmelioğluCENG Output : result is “Hearts" result is “Spades" result is “Diamonds" result is “Clubs"

strtok Example: char str[] = "Hearts-#-Spades-#-Diamonds-#-Clubs"; char delims[] = "#"; char *result; result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } Dr. Sadık EşmelioğluCENG Output : result is "Hearts-" result is "-Spades-" result is "-Diamonds-" result is "-Clubs"

strtok Example: char str[] = "Hearts-#-Spades-#-Diamonds-#-Clubs"; char delims[] = "#-"; char *result; result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } Dr. Sadık EşmelioğluCENG Output : result is "Hearts" result is “Spades" result is “Diamonds" result is “Clubs"

strtok Example: char str[] = “January 15, 2010"; char delims[] = “,"; char *result; result = strtok(str, delims); while(result != NULL) { printf("result is \"%s\"\n", result); result = strtok(NULL, delims); } Dr. Sadık EşmelioğluCENG Output : result is "January" result is "15" result is "2010"