Scientific Programming in C V. Strings

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

Chapter 8 - Characters and Strings

Chapter 8 C Characters and Strings

Strings and Library Functions

Characters and Strings

Fundamentals of Programming. Lecture 11: C Characters and Strings

Structured programming

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

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

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

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using 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

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

today cs3157-fall2002-sklar-lect05 1

Computer Programming

C mini reference. 5 Binary numbers 12

by Pearson Education, Inc. All Rights Reserved.

System Design and Programming II

C Characters and Strings

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

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

Contents. Preface. Introduction. Introduction to C Programming

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

Introduction to string

Course organization. Course introduction ( Week 1)

CS167 Programming Assignment 1: Shell

CSC 1107: Structured Programming

Arrays, Strings, & Pointers

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

CS1100 Introduction to Programming

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

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

Chapter 10 Characters, Strings, and the string class

Computers Programming Course 11. Iulian Năstac

Programming in C++ 4. The lexical basis of C++

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 11, FALL 2012

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

CS107 Spring 2019, Lecture 4 C Strings

SWEN-250 Personal SE. Introduction to C

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

Built-in Functions for NTCAs.

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

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

Section 3: Library Functions

8. Characters, Strings and Files

CS3157: Advanced Programming. Outline

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

Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

CS107, Lecture 4 C Strings

Strings and Stream I/O

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

CSE2301. Functions. Functions and Compiler Directives

Introduction to Programming Systems

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

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

Multiple Choice Questions ( 1 mark)

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

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

Computer Science XII Important Concepts for CBSE Examination Questions

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

LESSON 4. The DATA TYPE char

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

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

C Programming. Unit 9. Manipulating Strings File Processing.

LECTURE 15. String I/O and cstring library

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

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

1 Pointer Concepts. 1.1 Pointer Examples

ONE DIMENSIONAL ARRAYS

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Basic Elements of C. Staff Incharge: S.Sasirekha

Goals of this Lecture

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 in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

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

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

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Computer Programming: Skills & Concepts (CP) Strings

CS149: Elements of Computer Science. Fundamental C++ objects

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

CSC 270 Survey of Programming Languages. C-String Values

Create a Program in C (Last Class)

Strings. Steven R. Bagley

Java Basic Datatypees

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

Exercise 1.1 Hello world

TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

Split up Syllabus (Session )

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

BITG 1113: Array (Part 2) LECTURE 9

CCE1111 Programming for Engineers [C Programming Language]

Data Types and Variables in C language

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

CSCI0330 Intro Computer Systems Doeppner. Project C-Shell. Due: October 31, 2018 at 11:59pm. 1 Introduction 2

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Scientific Programming in C X. More features & Fortran interface

Softprocessor SP3: Subprograms and Header

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Transcription:

Scientific Programming in C V. Strings Susi Lehtola 1 November 2012

C strings As mentioned before, strings are handled as character arrays in C. String constants are handled as constant arrays. const char h e l l o []= Hello, world! ; const char h e l l o 2= Hello, world! ; The const qualifier (applicable to any datatype) states that the variable cannot be changed. const is the most misunderstood qualifier in C, since it can have different meanings. Scientific Programming in C, fall 2012 Susi Lehtola Strings 2/16

const example #i n c l u d e <s t d i o. h> i n t main ( void ) { / i n t e g e r / i n t i ; / i n t e g e r c o n s t a n t / const i n t c i =0; / p o i n t e r to i n t e g e r / i n t i p ; / p o i n t e r to c o n s t a n t i n t e g e r / const i n t c i p ; / c o n s t a n t p o i n t e r to i n t e g e r / i n t const i c p=&i ; / c o n s t a n t p o i n t e r to i n t e g e r c o n s t a n t / const i n t const c i c p=&c i ; } / Set p o i n t e r to i n t e g e r / i p=&i ; / Set p o i n t e r to c o n s t a n t i n t e g e r / c i p=&c i ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola Strings 3/16

const example, cont d #i n c l u d e <s t d i o. h> i n t main ( void ) { / i n t e g e r / i n t i ; / i n t e g e r c o n s t a n t / const i n t c i =0; / p o i n t e r to i n t e g e r / i n t i p ; / p o i n t e r to c o n s t a n t i n t e g e r / const i n t c i p ; } i p=&i ; / This i s a l s o a l l o w e d / c i p=i p ; / But to go i n the o t h e r way you need a c a s t. I t s g e n e r a l l y not a good i d e a to change c o n s t a n t s. / i p =( i n t ) c i p ; r e t u r n 0 ; Scientific Programming in C, fall 2012 Susi Lehtola Strings 4/16

static Another commonly used qualifier is static. When a global variable (defined outside of functions) or a function is defined as static, it is only visible to code inside the same source code file. External static can thus be used to hide implementation details. When an internal static is used for a variable (i.e., inside a function), the variable becomes permanently available inside the function: it is not created and destroyed every time the function is executed (analogous to the SAVE qualifier in Fortran). Scientific Programming in C, fall 2012 Susi Lehtola Strings 5/16

Strings, cont d Since in general there is no way to know the length of an array in C, strings are terminated with a special null character \0 in C. H e l l o w o r l d! \0 Because of the very low level approach to strings in C, people usually prefer to perform string manipulation in a higher level language. Scientific Programming in C, fall 2012 Susi Lehtola Strings 6/16

Special characters C has the following special characters: alert (bell) \a backspace \b form-feed \f carriage return \r horizontal tab \t vertical tab \v newline \n backslash \\ questionmark \? single quote \ double quote \ octal number \ooo octal number \xhh Scientific Programming in C, fall 2012 Susi Lehtola Strings 7/16

Character functions There are helper functions to determine what kind of a character is in question. #i n c l u d e <c t y p e. h> i n t i s a l n u m ( i n t c ) ; i n t i s a l p h a ( i n t c ) ; i n t i s c n t r l ( i n t c ) ; i n t i s d i g i t ( i n t c ) ; i n t i s g r a p h ( i n t c ) ; i n t i s l o w e r ( i n t c ) ; i n t i s p r i n t ( i n t c ) ; i n t i s p u n c t ( i n t c ) ; i n t i s s p a c e ( i n t c ) ; i n t i s u p p e r ( i n t c ) ; i n t i s x d i g i t ( i n t c ) ; Scientific Programming in C, fall 2012 Susi Lehtola Strings 8/16

Character functions, cont d isalnum() checks for an alphanumeric character, returns (isalpha(c) isdigit(c)). isalpha() checks for an alphabetic character. iscntrl() checks for a control character. isdigit() checks for a digit (0 through 9). isgraph() checks for any printable character except space. islower() checks for a lower-case character. isprint() checks for any printable character including space. ispunct() checks for any printable character which is not a space or an alphanumeric character. isspace() checks for white-space characters. isupper() checks for an uppercase letter. isxdigit() checks for a hexadecimal digits, that is, one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F. Scientific Programming in C, fall 2012 Susi Lehtola Strings 9/16

String functions There is also a bunch of string-related functions in C, available in the string.h header file. v o i d memcpy ( v o i d r e s t r i c t, c o n s t v o i d r e s t r i c t, s i z e t ) ; v o i d memset ( v o i d, i n t, s i z e t ) ; char s t r c a t ( char r e s t r i c t, c o n s t char r e s t r i c t ) ; i n t strcmp ( c o n s t char, c o n s t char ) ; char s t r c p y ( char r e s t r i c t, c o n s t char r e s t r i c t ) ; s i z e t s t r l e n ( c o n s t char ) ; Scientific Programming in C, fall 2012 Susi Lehtola Strings 10/16

memcpy #i n c l u de <s t r i n g. h> void memcpy ( void dest, const void s r c, s i z e t n ) ; The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap. Scientific Programming in C, fall 2012 Susi Lehtola Strings 11/16

memset #i n c l u de <s t r i n g. h> void memset ( void, int, s i z e t ) ; The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. Scientific Programming in C, fall 2012 Susi Lehtola Strings 12/16

strcat #i n c l u de <s t r i n g. h> char s t r c a t ( char r e s t r i c t, const char r e s t r i c t ) ; The strcat() function appends the src string to the dest string, overwriting the terminating null byte ( \0 ) at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. The strncat() function is similar, except that it will use at most n characters from src; and src does not need to be null-terminated if it contains n or more characters. Scientific Programming in C, fall 2012 Susi Lehtola Strings 13/16

strcmp #i n c l u de <s t r i n g. h> i n t strcmp ( const char s1, const char s2 ) ; i n t strncmp ( const char s1, const char s2, s i z e t n ) ; The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. The strncmp() function is similar, except it only compares the first (at most) n characters of s1 and s2. Scientific Programming in C, fall 2012 Susi Lehtola Strings 14/16

strcpy #i n c l u de <s t r i n g. h> char s t r c p y ( char dest, const char s r c ) ; char s t r n c p y ( char dest, const char s r c, s i z e t n ) ; The strcpy() function copies the string pointed to by src, including the terminating null byte ( \0 ), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated. Scientific Programming in C, fall 2012 Susi Lehtola Strings 15/16

strlen #i n c l u de <s t r i n g. h> s i z e t s t r l e n ( const char s ) ; The strlen() function calculates the length of the string s, excluding the terminating null byte ( \0 ). Scientific Programming in C, fall 2012 Susi Lehtola Strings 16/16