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

Similar documents
Computers Programming Course 11. Iulian Năstac

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

by Pearson Education, Inc. All Rights Reserved.

Introduction to string

Create a Program in C (Last Class)

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

ONE DIMENSIONAL ARRAYS

Scientific Programming in C V. Strings

Introduction to C/C++ Lecture 5 - String & its Manipulation

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

Computer Programming: Skills & Concepts (CP) Strings

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

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


C Characters and Strings

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

CS107 Spring 2019, Lecture 4 C Strings

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

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

CS107, Lecture 4 C Strings

Chapter 8 Character Arrays and Strings

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

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

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

8. Characters, Strings and Files

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

LECTURE 15. String I/O and cstring library

Chapter 8 C Characters and Strings

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

Fundamentals of Programming

Chapter 8 - Characters and Strings

Fundamental of Programming (C)

Characters and Strings

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

System Design and Programming II

CCE1111 Programming for Engineers [C Programming Language]

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

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Chapter 13. Strings. Introduction

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

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

C: How to Program. Week /May/28

Fundamentals of Programming & Procedural Programming

Arrays, Strings, & Pointers

Computers Programming Course 10. Iulian Năstac

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

Object Oriented Programming COP3330 / CGS5409

C mini reference. 5 Binary numbers 12

Computer Programming

Built-in Functions for NTCAs.

(9-3) Strings II H&K Chapter 8. Instructor - Andrew S. O Fallon CptS 121 (October 19, 2018) Washington State University

Procedural Programming

Exercise 1.1 Hello world

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture7 Strings and Characters

C Programming Language Review and Dissection III

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

Week 8 Lecture 3. Finishing up C

Computers Programming Course 12. Iulian Năstac

Course organization. Course introduction ( Week 1)

BITG 1113: Array (Part 2) LECTURE 9

Data Structures and Algorithms(4)

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

String can be represented as a single-dimensional character type array. Declaration of strings

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

Structured programming

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

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C-String Library Functions

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Functions: call by reference vs. call by value

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

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.

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

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture5 C Characters and Strings

Pointers, Arrays, and Strings. CS449 Spring 2016

CSC 2400: Computer Systems. Arrays and Strings in C

1 Pointer Concepts. 1.1 Pointer Examples

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

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

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

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

EECE.2160: ECE Application Programming Fall 2017

LAB7 : Characters and Strings

Principles of C and Memory Management

CSC 2400: Computer Systems. Arrays and Strings in C

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

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

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

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

9/9/2017. Prof. Vinod Mahajan

Chapter 21: Introduction to C Programming Language

Chapter 10: Characters, C- Strings, and More About the string Class Character Testing

Lecture 05 Pointers ctd..

C Programming. Unit 9. Manipulating Strings File Processing.

Arrays and Strings. CS449 Fall 2017

Chapter 5 - Pointers and Strings

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Transcription:

CHARACTER STRINGS 1. Overview The learning objective of this lab session is to: Understand the internal representation of character strings Acquire skills in manipulating character strings with standard string handling functions. 2. Brief theory reminder 2.1. Internal memory representation of a character string A character string is stored as one-dimensional array of char type. Each character is represented on a byte by its ASCII code. The last character in the string is the null character ( \0 ). The name of the array that stores the string is a constant pointer of the character string. We have the following relations: string[i], where i [0,16] represents the ASCII code of the i th character of the string; string + i, where i [0,16] is the address of the i th character of the string; *( string + i ) has the same effect as string[i]. A string may be declared also as follows: char *const string= CHARACTER STRING ; An array of strings may be declared as: char *tab[]=string_0, string _1,, string _n; In this case, tab[i], for i [0, n], is a pointer to the string string_i. The statement: printf( %s\n, tab[i]); will display the text string_i. 2.2. Standard string handling functions The standard input/output functions for strings are: - gets/puts; - scanf/printf; - sscanf/sprintf They were presented in Lab. 1. T.U. Cluj-Napoca Computer Programming 1

Next we present string handling functions with prototype in string.h. 2.2.1. String length The length of a string (without \0 ) is returned by the function strlen, having the prototype: unsigned strlen (const char *s); /* Program L8Ex1.c */ /* The usage of the function strlen */ #include <conio.h> #define alpha "Press a key!" char string1[]="string OF CHARACTERS"; char * string2="string OF CHARACTERS"; int n1, n2, n3; n1=strlen(string1); n2=strlen(string2); n3=strlen("string OF CHARACTERS"); /* The values of n1, n2 and n3 are the same, i.e. 20 */ printf("\n n1=%d n2=%d n3=%d\n", n1, n2, n3); printf("%s\n",alpha); 2.2.2. String copy To copy a string, from a source memory area (of address source) into another memory area (of address dest) you can use the function strcpy, having the prototype: char *strcpy (char *dest, const char *source); Notes: The copy includes the ASCII null character. The function returns the address of the destination. To copy of no more than n characters of a string from a source memory area (having the address source) into another memory area (having the address dest) you can use the function strncpy, having the prototype: char *strncpy (char *dest, const char *source, unsigned n); After the last character that is transferred, you must append a null ASCII character ( \0 ). Obviously, if n is greater than the length of the source string, the entire source string is copied. /* Program L8Ex2.c */ T.U. Cluj-Napoca Computer Programming 2

/* Usage of the function strcpy */ char string1[]="string OF CHARACTERS"; char *string2="string OF CHARACTERS"; char string3[100], string4[100], string5[100]; strcpy(string3, string1); printf("\nstring3: %s\n", string3); strcpy(string4, "Standard string handling functions"); printf("\nstring4: %s\n", string4); strncpy(string5, string2, 9); /* string5 contains STRING OF */ string5[6]='\0'; printf("\nstring5: %s\n", string5); 2.2.3. String concatenation Appending a source character string, located in a memory area of address source, after the last character before \0 of another string located in a memory area of dest, is done using the function strcat, having the prototype: char *strcat(char *dest, const char *source); It is important to put the ASCII character null ( \0 ) at the end of the resulting string. The function returns the address of the destination. You can take only n characters from the source string, using the function strncat, having the prototype: char *strncat (char *dest, const char *source, unsigned n); The null ASCII character is automatically appended to the result string. If n is greater than the length of the source string, strncat has the effect of strcat. /* Program L8Ex3.c */ /* Usage of the function strcat */ char string1[]="string1 OF CHARACTERS"; char *string2="string2 OF CHARACTERS"; char string3[100]; int i; strcpy(string3, string1); T.U. Cluj-Napoca Computer Programming 3

strcat(string1, string2); printf("\nstring1: %s\n", string1); strncat(string3, string2, 5); /* After the last character of the string string3, '\0' is placed by default */ for (i=0; i <= strlen(string3)+1; ++i) printf("%x", string3[i]); printf("\n string3: %s\n", string3); 2.2.4. String comparison The comparison of two string is done by taking sequentially the pairs of characters located on the i th position in the compared strings, based on their ASCII codes, until: - the i th character in the first string is reached, and it s different from the corresponding i th character from the second string; - the end of one of the compared strings, or the end of both strings is reached. The comparison is done using the function strcmp having the prototype: int strcmp(const char *string1, const char * string2); This function returns: - a negative value if the string having the address string1 is less than the string having the address string2; - zero if the two strings are equal; - a positive value if the string having the address string1 is greater than the string having the address string2; If only the first n characters from the two strings are to be compared, use the function strncmp having the prototype: int strncmp (const char * string1, const char * string2, unsigned n); If the lowercase and uppercase letters are considered identical, then use the corresponding functions: int stricmp (const char *sir1, const char *sir2); int strnicmp (const char *sir1, const char *sir2, unsigned n); /* Program L8Ex4.c */ /* The usage of strcmp function*/ char string1[100]="string OF CHARACTERS"; char *string2="string of characters"; int i, j, k, l; i=strcmp(string1, string2); /* i<0, then string1< string2 */ printf("\ni=%d\n", i); T.U. Cluj-Napoca Computer Programming 4

j=strncmp(string1, string2, 3); /* j=0, then the first 3 characters from string1 and string2 are equal */ printf("\nj=%d\n", j); k=stricmp(string1, string2); /* k=0, the two strings are equal */ printf("\nk=%d\n", k); l=strnicmp(string1, "STRING of 22 characters", 6); /*l=0 */ printf("\nl=%d\n", l); 3. Lab Tasks 3.1. Analyze and execute the examples provided above. 3.2. Write a function to extract, from a source string, a substring identified by the position in the source string and by the length expressed as a number of characters. 3.3. Write a function to insert a source character string in the context of a destination character string, in a given position. 3.4. Write a function to delete a substring from a given character string, specifying the beginning position and the length of the substring. 3.5. Write a function to verify if a given string is a substring of another character string. If it is, specify the beginning position of the substring. 3.6. Write two functions, the first to convert an integer or real number into a string of characters, and the second to perform the inverse operation. 3.7. Write a program to read n strings of characters and display both the longest string and the biggest one as seen as an alphanumeric sequence. 3.8. Read from the keyboard the author, the title and the publication year for a number of n books. Display the following: a) the names of the authors in alphabetic order; b) the names of the authors and the titles of their books in order of the publication year. 3.9. Read from the keyboard the names of n kings and the corresponding year limits of their state leading periods. Display the name of all the kings in alphabetic order, and the number of years they ruled. 3.10. Read from the keyboard strings of at most 80 characters, representing integer or real nonexponential numbers, separated by spaces. Compute the sum of the real numbers and of the integer numbers of each string, except the incorrect values. An incorrect value is a character string delimited by spaces, containing non-numeric characters, or having the length greater than 5. T.U. Cluj-Napoca Computer Programming 5