CCE1111 Programming for Engineers [C Programming Language]

Similar documents
Introduction to string

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Computer Systems and Networks

QuickSort. CIS 15 : Spring 2007

Softprocessor SP3: Subprograms and Header

Lecture 17. Strings II. CptS 121 Summer 2016 Armen Abnousi

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

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

Chapter 8 - 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

Intermediate Programming, Spring 2017*

Computer Systems and Networks

C: How to Program. Week /May/28

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

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

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

by Pearson Education, Inc. All Rights Reserved.

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.

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

today cs3157-fall2002-sklar-lect05 1

Strings and Library Functions

BITG 1113: Array (Part 2) LECTURE 9

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

C mini reference. 5 Binary numbers 12

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

Chapter 8 C Characters and Strings

Computer Systems and Networks. ECPE 170 Jeff Shafer University of the Pacific. C Programming

Structured programming

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

Scientific Programming in C V. Strings

Chapter 10 Characters, Strings, and the string class

LAB7 : Characters and Strings

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

C Characters and Strings

C-String Library Functions

Computer Programming

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

ONE DIMENSIONAL ARRAYS


Strings. Daily Puzzle

Computers Programming Course 10. Iulian Năstac

System Design and Programming II

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

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS 222: Pointers and Manual Memory Management

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

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

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

Characters and Strings

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

Computers Programming Course 11. Iulian Năstac

Exercise 1.1 Hello world

Fundamentals of Programming. Lecture 11: C Characters and Strings

Week 2. Muhao Chen.

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

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

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.

CS107 Spring 2019, Lecture 4 C Strings

Chapter 8 Character Arrays and Strings

C PROGRAMMING. Characters and Strings File Processing Exercise

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

Multiple Choice Questions ( 1 mark)

Computer Systems and Networks. ECPE 170 University of the Pacific

CS107, Lecture 4 C Strings

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

Programming Language Reference

Object oriented programming C++

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

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

Copyright 2003 Pearson Education, Inc. Slide 1

SWEN-250 Personal SE. Introduction to C

UNIT-I Input/ Output functions and other library functions

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

CSC 221: Computer Programming I. Fall 2001

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Split up Syllabus (Session )

Unit 2: The string class

LECTURE 15. String I/O and cstring library

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

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

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

Course organization. Course introduction ( Week 1)

Approximately a Test II CPSC 206

Appendix A Developing a C Program on the UNIX system

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

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

Part II Answer to all the questions (2 Marks):

1 Pointer Concepts. 1.1 Pointer Examples

C Language: Review. INFO High Performance Scientific Computing. 26 septembre 2017

Presented By : Gaurav Juneja

C-LANGUAGE CURRICULAM

Create a Program in C (Last Class)

CSE2301. Functions. Functions and Compiler Directives

Introduction to Programming Systems

8. Characters, Strings and Files

Transcription:

are collection of characters. Unfortunately C does not has a primitive data type for strings and therefore an array of characters is used to represent a string. The following code snippet illustrates a string declaration. Notice that since a string is represented as an array of characters, it can be assigned a value using the equal s operator only when during the declaration. Consider the following two examples. The first example is correct while the second one is wrong: To assign a text value to a string, the function strcpy has to be used as shown in the example below. Note that in order to use this function, the header file (library) should be included in your program. This library contains other important functions that are related to string handling. Matthew Xuereb 2012/2013 1

In C, a string always ends with the null (\0) character. So if for instance the following string is declared: char name[10] = Joe ; the following will be stored in memory: Notice the null character (\0) at element 3. It is very important that every string will be long enough to allow C to put the null character at the end. Note that if there is not enough space for the null character then C will not know which is the end of the string an it can therefore display garbage characters on the screen when displayed. Some modern C compilers (such as the one used by MS Visual C++ Express) will generate and error. C standard string functions The basic C language comes with a variety of standard functions for processing strings. The following table and examples illustrates the mostly used functions with an example: Function Description Library strcpy Copies one string to another. <string.h> strlen Returns the length of a string. <string.h> strcat Adds the characters of one string to the end of another. <string.h> strcmp Compares to strings - case sensitive. <string.h> strnicmp Compares to strings - case insensitive. <string.h> strcspn Returns the position of a character in the string from a specified substring. <string.h> atoi Calculates the integer value of a string. <stdlib.h> atof Calculates the float value of a string. <stdlib.h> Matthew Xuereb 2012/2013 2

strcpy Copies one string to another. see example above strlen Returns the length of a string. strcat Adds the characters of one string to the end of another. Matthew Xuereb 2012/2013 3

strcmp Compares to strings - case sensitive. strnicmp Compares to strings - case insensitive. Note that with the function strnicmp, an extra argument is required. This argument indicates up till which number to compare case insensitive. Matthew Xuereb 2012/2013 4

strcspn Returns the position of a character in the string from a specified substring. atoi and atof These two functions return the integer and float values of a string respectively. C accessing a string as an array of characters As in C a string is an array of characters, it can be manipulated and accessed character by character. In the following example a string is accessed character by character and each character is displayed separately on a new line: Matthew Xuereb 2012/2013 5

Converting a string to uppercase In C, there is a function that can be used to convert a given character to uppercase. This is the toupper() function. It can be used in conjunction with a for loop to convert a particular string to uppercase. Consider the following example: Note that in order to use the toupper() function, the library ctype.h has to be included in the program as shown above. This library has a number of functions that can be used on a character and these are explained below. ctype.h The ctype library has two types of functions the to functions are used to convert the character to another character while the is functions checks for particular properties of a character. The following table lists the functions found in this library: Function isalnum(char) isalpha(char) iscntrl(char) Description Checks if a character is an alphabetic letter or a digit Checks if a character is an alphabetic letter Checks if a character is a control character Matthew Xuereb 2012/2013 6

isdigit(char) Checks if a character is a digit isgraph(char) Checks if a character is a pointing character except for the space character islower(char) Checks if a character is lowercase isprint(char) Checks if a character is a character that can be displayed on the screen ispunct(char) Checks if a character is a punctuation character isspace(char) Checks if a character is a is a which space isupper(char) Checks if a character is uppercase isxdigit(char) Checks if a character is a hexadecimal digit tolower(char) Converts a character to lowercase toupper(char) Converts a character to uppercase Consider the following example that checks whether an inputted character is a digit or an alphabetic character, or otherwise: Matthew Xuereb 2012/2013 7