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

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

Principles of C and Memory Management

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

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

Introduction to string

Fundamentals of Programming

Chapter 8 Character Arrays and Strings

Fundamental of Programming (C)

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


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

ONE DIMENSIONAL ARRAYS

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

LAB7 : Characters and Strings

Write a C program using arrays and structure

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

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

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

ARRAYS(II Unit Part II)

Fundamentals of Programming & Procedural Programming

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

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

Functions: call by reference vs. call by value

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

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

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

Procedural Programming

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

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

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

C Characters and Strings

C does not support string data type. Strings in C are represented by arrays of characters.

8. Characters, Strings and Files

Array Initialization

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

Procedural Programming & Fundamentals of Programming

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

CMPSC 311- Introduction to Systems Programming Module: Strings

UNIT 2 STRINGS. Marks - 7

C Programming Language

Arrays, Strings, & Pointers

Strings and Library Functions

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

CSC 2400: Computer Systems. Arrays and Strings in C

Week 8 Lecture 3. Finishing up C

UNIT III ARRAYS AND STRINGS

Scientific Programming in C V. Strings

CS107 Spring 2019, Lecture 4 C Strings

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Structured programming

CSC 2400: Computer Systems. Arrays and Strings in C

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

Computers Programming Course 11. Iulian Năstac

Computer Programming: Skills & Concepts (CP) Strings

Chapter 10. Arrays and Strings

CS107, Lecture 4 C Strings

Intermediate Programming, Spring 2017*

Lecture 12. We have already used strings. Strings. Hello Class is a string.

Chapter 13. Strings. Introduction

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1

Dynamic arrays / C Strings

Engineering Problem Solving with C++, Etter

Computers Programming Course 10. Iulian Năstac

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

20 Dynamic allocation of memory: malloc and calloc

Create a Program in C (Last Class)

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

BCSE1002: Computer Programming and Problem Solving LAB MANUAL

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

7.STRINGS. Data Structure Management (330701) 1

Programming Language B

Built-in Functions for NTCAs.

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

CSCE 548 Building Secure Software Buffer Overflow. Professor Lisa Luo Spring 2018

For example, let s say we define an array of char of size six:

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

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

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

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

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

Department of Computer Science and Engineering. Programming for Problem Solving. I Year B.Tech. (I - Sem) Assistant Professor (M.

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

C Programming. Unit 9. Manipulating Strings File Processing.

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

CS1100 Introduction to Programming

C programming basics T3-1 -

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

(2½ Hours) [Total Marks: 75

Module: Safe Programming. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

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

Lecture 05 Pointers ctd..

1 Pointer Concepts. 1.1 Pointer Examples

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

For instance, we can declare an array of five ints like this: int numbers[5];

Transcription:

Eastern Mediterranean University School of Computing and Technology Information Technology Lecture7 Strings and Characters Using Strings The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null. The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello." char msg[6]= h, e, l, l, o ; If you follow the rule of array initialization then you can write the above statement as follows: char msg[6]= Hello ; A string of characters is stored in successive elements of a character array and terminated by the NULL character. For example, the string "Hello" is stored in a character array, msg[], as follows: char msg[6]= Hello ; msg[0] = 'H'; msg[1] = 'e'; msg[2] = 'l'; msg[3] = 'l'; msg[4] = 'o'; msg[5] = '\0'; If you want the user to input his or her name, you must use a string. Using scanf() to input a string works, but it will terminate the string after it reads the first space. There are several approaches to handling this problem, but probably the simplest and safest is to use the gets function. Following is the memory presentation of above defined string in C/C++: 1 P a g e

Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array. Let us try to print above mentioned string: char msg[6] = 'H', 'e', 'l', 'l', 'o', '\0'; printf("greeting message: %s\n", msg ); When the above code is compiled and executed, it produces result something as follows: Greeting message: Hello Ex: This program reads the word character by character and then prints out the string char str[20]="hello"; int i=0; while(str[i]!='\0') putchar(str[i]); i++; printf("\n"); Ex: This program reads characters until a newline, stores them in an array and terminates the string with a NULL character. It then prints out the string. 2 P a g e

char str[20],ch; int i=0; printf("enter some characters:\n"); ch=getchar(); while(ch!='\n') str[i]=ch; i++; ch=getchar(); //*(str+i)=ch; str[i]='\0'; // *(str+i)=null; or *(str+i)=0; printf("\nthe string is:\n"); i=0; while(str[i]!='\0') putchar(str[i]); // putchar(*(str+i)); i++; //puts(str); printf("\n"); Strings and Pointers Below is how you might use a character pointer to keep track of a string. char label[] = "Single"; char label2[10] = "Married"; char *labelptr; labelptr = label; We would have something like the following in memory (e.g., supposing that the array label started at memory address 2000, etc.): label @2000 ------------------------------ S i n g l e \0 ------------------------------ label2 @3000 ------------------------------------------ M a r r i e d \0 ------------------------------------------ labelptr @4000 -------- 2000 -------- 3 P a g e

Passing Strings Just as we can pass other kinds of arrays to functions, we can do so with strings. Ex: void func(char *p) int i=0; while(*(p+i)!='\0') putchar(*(p+i)); i++; printf("\n"); char str[25]; printf("enter a message:\n"); gets(str); func(str); C supports a wide range of functions that manipulate null-terminated strings: S.N. Function & Purpose 1 2 3 4 strcpy(s1, s2); Copies string s2 into string s1. strcat(s1, s2); Concatenates string s2 onto the end of string s1. strlen(s1); Returns the length of string s1. strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. Strlen(str) function The C library function strlen(str) computes the length of the string str up to but not including the terminating null character. Parameters str -- This is the string whose length is to be found. 4 P a g e

Example The following example shows the usage of strlen() function. char str[50]; int len; strcpy(str, "This is example"); len = strlen(str); printf("length of %s is %d \n", str, len); Length of This is an example is 26 Strcpy(dest,src) function The C library function strcpy(dest,src) copies the string pointed to by src to dest. Parameters dest -- This is the pointer to the destination array where the content is to be copied. src -- This is the string to be copied. Example The following example shows the usage of strcpy() function. char src[40]; char dest[100]; strcpy(src, "This is example "); strcpy(dest, src); printf("final copied string : %s\n", dest); Final copied string : This is an example 5 P a g e

Strcat(dest,src) function The C library function strcat(dest, src) appends the string pointed to by src to the end of the string pointed to by dest. Parameters dest -- This is pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string. src -- This is the string to be appended. This should not overlap destination. Example The following example shows the usage of strcat() function. char src[50], dest[50]; strcpy(src, "This is source"); strcpy(dest, "This is destination "); strcat(dest, src); printf("final destination string : %s \n", dest); Final destination string : This is destination This is source Strcmp(dest,src) function The C library function strcmp(str1, str2) compares the string pointed to by str1 to the string pointed to by str2. Parameters str1 -- This is the first string to be compared. str2 -- This is the second string to be compared. Return Value This function returned values are as follows: 6 P a g e if Return value if < 0 then it indicates str1 is less than str2 if Return value if > 0 then it indicates str2 is less than str1

Example if Return value if = 0 then it indicates str1 is equal to str2 The following example shows the usage of strncmp() function. char str1[15]; char str2[15]; int ret; strcpy(str1, "abcdef"); strcpy(str2, "ABCDEF"); ret = strcmp(str1, str2); if(ret > 0) printf("str1 is less than str2\n"); else if(ret < 0) printf("str2 is less than str1\n"); else printf("str1 is equal to str2\n"); str1 is less than str2 strrev() function strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is given below. strrev(string); strrev( ) function is non standard function which may not available in standard library in C. 7 P a g e

Example program for strrev() function in C: In below program, string Hello is reversed using strrev( ) function and output is displayed as olleh. char name[30] = "Hello"; printf("string before strrev( ) : %s\n",name); printf("string after strrev( ) : %s\n",strrev(name)); String before strrev(): hello String after strrev(): olleh Following example makes use of few of the above-mentioned functions: char str1[12] = "Hello"; char str2[12] = "World"; char str3[12]; int len ; /* copy str1 into str3 */ strcpy(str3, str1); printf("strcpy( str3, str1) : %s\n", str3 ); /* concatenates str1 and str2 */ strcat( str1, str2); printf("strcat( str1, str2): %s\n", str1 ); /* total lenghth of str1 after concatenation */ len = strlen(str1); printf("strlen(str1) : %d\n", len ); When the above code is compiled and executed, it produces result something as follows: strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10 8 P a g e