Introduction to string

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

C Characters and Strings

Chapter 8 Character Arrays and Strings

Chapter 8 - Characters and Strings

Chapter 8 C Characters and Strings

LECTURE 15. String I/O and cstring library

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

ONE DIMENSIONAL ARRAYS

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

C: How to Program. Week /May/28

CCE1111 Programming for Engineers [C Programming Language]

Strings and Library Functions

Computers Programming Course 11. Iulian Năstac

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

Course organization. Course introduction ( Week 1)

Strings. Steven R. Bagley

Characters and Strings

Object Oriented Programming COP3330 / CGS5409

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

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

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

Structured programming

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

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

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

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

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.

Computer Science & Engineering 150A Problem Solving Using Computers

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Functions: call by reference vs. call by value

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

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

BITG 1113: Array (Part 2) LECTURE 9

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

Intermediate Programming, Spring 2017*

by Pearson Education, Inc. All Rights Reserved.

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.

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

Scientific Programming in C V. Strings

C mini reference. 5 Binary numbers 12

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

CMPSC 311- Introduction to Systems Programming Module: Strings

8. Characters, Strings and Files

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

System Design and Programming II

9/9/2017. Prof. Vinod Mahajan

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


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

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

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

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

CSC 2400: Computer Systems. Arrays and Strings in C

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

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

ARRAYS(II Unit Part II)

Strings. Daily Puzzle

CS107 Spring 2019, Lecture 4 C Strings

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Fundamentals of Programming

Chapter 10 Characters, Strings, and the string class

CS107, Lecture 4 C Strings

CSC 2400: Computer Systems. Arrays and Strings in C

Multiple Choice Questions ( 1 mark)

C PROGRAMMING. Characters and Strings File Processing Exercise

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

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

Fundamental of Programming (C)

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Fundamentals of Programming & Procedural Programming

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

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

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

UNIT 2 STRINGS. Marks - 7

C Programming. Unit 9. Manipulating Strings File Processing.

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

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

C Programming Multiple. Choice

Chapter 21: Introduction to C Programming Language

Library Functions. General Questions

Model Viva Questions for Programming in C lab

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

LAB7 : Characters and Strings

today cs3157-fall2002-sklar-lect05 1

UNIT-I Input/ Output functions and other library functions

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Procedural Programming & Fundamentals of Programming

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

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

Arrays, Strings, & Pointers

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

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

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

A First Book of ANSI C Fourth Edition. Chapter 9 Character Strings

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-LANGUAGE CURRICULAM

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

Transcription:

1

Introduction to string String is a sequence of characters enclosed in double quotes. Normally, it is used for storing data like name, address, city etc. ASCII code is internally used to represent string in memory. In C, each string is terminated by a special character called null character represented as \0 or NULL. Because of this reason, the character array must be declared one size longer than the string required to be stored. char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; char greeting[] = "Hello"; 2

Reading strings The format specifier %s is used to read a string in the character array. For example, char name[20]; scanf( %s,name); OR gets(name); Unlike previous scanf calls, in the case of character arrays, the ampersand (&) is not required before the variable name. The advantage of gets( ) is that we can read string involving blanks, tabs. While the scanf( ) reads only upto blank or tab character. So, scanf( ) is used to read word while gets( ) can be used read sentence involving many words. 3

printing strings The format specifier %s is used to print a string in the character array. For example, char name[ ] = Hello ; printf( %s,name); OR puts(name); puts( ) can print only one string at a time, while one printf( ) can print multiple strings by using %s many times in the printf( ). char name[ ] = ABC ; char surname[ ] = XYZ ; printf( %s %s, name, surname); OR puts(name); puts(surname); 4

5

Arithmetic operations on characters To write a character in its integer representation, we may write it as an integer. For example, if the machine uses the ASCII representation, then, x = a ; printf( %d \n, x); will display the number 97 on the screen. It is possible to perform arithmetic operations on the character constants and variables. For example, x = z 1 This is a valid statement. In ASCII, the value of z is 122, and therefore, the statement will assign the value 121 to the variable x. We may also use character constants in relational expressions. For example, ch >= A && ch <= Z 6

Distinction between characters and strings The representation of a char (e.g., Q ) and a string (e.g., Q ) is essentially different. A string is an array of characters ended with the null character. Q Character Q Q \0 String Q 7

String manipulation functions Sr. No String function Syntax Explanation 1. strlen( ) strlen(s1) Gives the length of input string (s1). 2. strcpy( ) strcpy(dest, source) Copies source string into destination string. 3. strncpy( ) strncpy(dest, source,n) Copies given number of characters (n) of source string to destination string. 4. strcat( ) strcat(s1, s2) Concatenates s2 at the end of s1. 5 strncat( ) strncat(s1, s2,n) Appends portion of s2 at the end of s1 as specified by n. 6. strcmp( ) strcmp(s1, s2) Returns 0 if s1 is same as s2. Returns < 0 if s1 < s2. Returns > 0 if s1 > s2. Is case-sensitive. 7. strcmpi( ) strcmpi(s1, s2) Same as strcmp( ) function. But, this function negotiates case. A and a are treated as same. Is case-insensitive. 8. strncmp( ) strncmp(s1, s2, n) It compares both the string till n characters or in other words it compares first n characters of both the strings. 9. strnicmp( ) strnicmp(s1, s2, n) Same as strncmp( ). But is case insensitive. 10. strrev( ) strrev(s1) Returns the reverse of the string. 8

strlen( ) In C, strlen() function calculates the length of string. It is defined under "string.h" header file. It takes only one argument, i.e, string name. Syntax of strlen() Function strlen() returns the value of type integer. 9

strlen( ) 10

Finding length of string without using strlen( ) 11

strlen vs sizeof strlen returns you the length of the string stored in array, however sizeof returns the total allocated size assigned to the array. Consider the given example, then the following statements would return the below values. strlen(str1) returned value 11. sizeof(str1) would return value 20 as the array size is 20 (see the first statement in main function). 12

strcpy( ) Function strcpy() copies the content of one string to the content of another string. It is defined under "string.h" header file. It takes two arguments. Syntax of strcpy() Here, source and destination are both the name of the string. This statement, copies the content of string source to the content of string destination. 13

strcpy( ) 14

Copy one string to another without using strcpy( ) 15

strncpy( ) Function strncpy() copies the content of one string to the content of another string by specifying the number of characters to copy. It is defined under "string.h" header file. It takes two arguments. Syntax of strncpy() strncpy(dest, src, n); After using this function, you need to manually put null character at the end of destination string dest[n] = \0 ; Here, source and destination are both the name of the string. This statement, copies the content of string source to the content of string destination. 16

strncpy( ) 17

Copy a string without using strncpy( ) 18

strcat( ) In C programming, strcat() concatenates(joins) two strings. It takes two arguments, i.e, two strings and resultant string is stored in the first string specified in the argument. Function strcat() is defined under "string.h" header file. Syntax of strcat() strcat(first_string,second_string); 19

Concatenate two strings without using strcat( ) 20

strncat( ) It concatenates n characters of str2 to string str1. A terminator char ( \0 ) will always be appended at the end of the concatenated string. 21

strcmp( ) C does not permit the comparison of two strings directly. That is, the statements such as if (name1 == name2) if (name1 == ABC ) are not permitted. It is therefore necessary to compare the two strings to be tested, character by character. The comparison is done until there is a mismatch or one of the strings terminates into a null character, whichever occurs first. In C, strcmp() compares two string and returns value 0, if the two strings are equal. It is defined under "string.h" header file. Function strcmp() takes two arguments, i.e, name of two string to compare. Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. Syntax of strcmp() temp_variable = strcmp(str1, str2); 22

strcmp( ) 23

strcmp( ) If two strings are not equal, strcmp() returns positive value i.e. 1 if ASCII value of first mismatching element of first string is greater than that of second string and negative value i.e. -1 if ASCII value of first mismatching element of first string is less than that of second string. For example: Since, ASCII value of 'a' is less than that of 'c', variable temp will be negative. 24

Compare two strings without using strcmp( ) 25

strcmpi( ) strcmpi( ) function in C is same as strcmp() function. But, strcmpi( ) function is not case sensitive. i.e, A and a are treated as same characters. Where as, strcmp() function treats A and a as different characters. 26

Some String programs Write a program to delete a character entered by the user from the input string. All occurrences of the input character should be deleted from the string. Write a program to perform right trim, left trim and all trim for the input string. Write a program to perform the following functions using a menu based functionality: Insert a word in the given string. Find a word in the given string and print the occurrences of the word in given string. Delete a word from a given string. Replace a given word from a given string. Write a program to compare case insensitive strings. Write a program to suppress all capital letters at the bottom of character array. E.g. input is Nirma University, output should be Irma niversitynu 27

Program to count the number of consonants, vowels, special characters, white spaces, words and digits in a given line of text 28

Program to swap even positioned characters with odd positioned characters in a given string 29

getchar( ) and putchar( ) The int getchar(void) function reads the next available character from the screen and returns it as an integer. This function reads only single character at a time. The int putchar(int c) function puts the passed character on the screen and returns the same character. This function puts only single character at a time. 30

Conversions Between Strings Numbers The <stdlib.h> defines some basic functions for conversion from strings to numbers: atoi( 123 ) converts a string to an integer. atol( 123 ) converts a string to a long integer. atof( 12.3 ) converts a string to a float. 31

Conversions Between Strings Numbers The <stdlib.h> defines some basic functions for conversion from numbers to strings: itoa( ) function in C language converts int data type to string data type. ltoa( ) function in C language converts long data type to string data type. 32

Character Analysis and Conversion The <ctype.h> library defines facilities for character analysis and conversion. Functions int isalnum(int c) int isalpha(int c) int isdigit(int c) int islower(int c) int isupper(int c) int toupper(int c) int tolower(int c) Description This function checks whether the passed character is alphanumeric. This function checks whether the passed character is alphabetic. This function checks whether the passed character is decimal digit. This function checks whether the passed character is lowercase letter. This function checks whether the passed character is an uppercase letter. This function converts lowercase letters to uppercase. This function converts uppercase letters to lowercase. 33

sscanf( ) function sscanf() function is used to extract strings from the given string. Consider, char str[20] = "Online C Program"; If we want to extract " Online", "C" and " Program " in a different variable then it can be done using sscanf function. Syntax: sscanf(characterarray, "Conversion specifier", address of variables); This will extract the data from the character array according to the conversion specifier and store into the respective variables. char str[20] = " Online C Program "; char first[20], second[20], third[20]; sscanf(str, "%s %s %s",first,second,third); In the above example, " Online" will get stored in variable first, "C" will get stored in variable second, "Program" will get stored in variable third. 34

sprintf( ) function sprintf() function is exactly opposite to sscanf() function. sprint() function writes the formatted text to a character array. Syntax: sprintf (CharacterArray,"Conversion Specifier", variables); Let us understand this using an example. char str[20]; char first[20] = " Online", second[20] = "C", third[20] = "Program"; sprintf(str, "%s %s %s",first,second,third); In the above example, Online C Program will get stored in the character array str. 35

36