Strings and Library Functions

Similar documents
Scientific Programming in C V. Strings

UNIT-I Input/ Output functions and other library functions

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.

Multiple Choice Questions ( 1 mark)

Chapter 8 - Characters and Strings

Fundamentals of Computer Programming Using C

Introduction to string

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

C: How to Program. Week /May/28

Strings. Daily Puzzle

Fundamentals of Programming. Lecture 11: C Characters and Strings

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

Chapter 8 Character Arrays and Strings

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Chapter 8 C Characters and Strings

today cs3157-fall2002-sklar-lect05 1

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

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

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

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

LESSON 4. The DATA TYPE char

Characters and Strings

Chapter 10 Characters, Strings, and the string class

Structured programming

C mini reference. 5 Binary numbers 12

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

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

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

Approximately a Test II CPSC 206

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

Intermediate Programming, Spring 2017*

Course organization. Course introduction ( Week 1)

Computer Science XII Important Concepts for CBSE Examination Questions

C Programming Multiple. Choice

ARRAYS(II Unit Part II)

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

LAB7 : Characters and Strings

ONE DIMENSIONAL ARRAYS

Vidyalankar. F.E. Sem. II Structured Programming Approach Prelim Question Paper Solution

C Programming. Unit 9. Manipulating Strings File Processing.

CS3157: Advanced Programming. Outline

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

SWEN-250 Personal SE. Introduction to C

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

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

Basics of Programming

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

Computers Programming Course 11. Iulian Năstac

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

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

CCE1111 Programming for Engineers [C Programming Language]

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Arrays, Strings, & Pointers

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

System Design and Programming II

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

8. Characters, Strings and Files

Chapter 1 Getting Started Structured Programming 1

Standard prelude. Appendix A. A.1 Classes

Contents. Preface. Introduction. Introduction to C Programming

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Split up Syllabus (Session )

EM108 Software Development for Engineers

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

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

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Computer Language. It is a systematical code for communication between System and user. This is in two categories.

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

Goals of this Lecture

Reminder. Sign up for ee209 mailing list. Precept. If you haven t received any from ee209 yet Follow the link from our class homepage

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CSC 270 Survey of Programming Languages. C-String Values

Introduction. What is function? Multiple functions form a larger program Modular programming

Strings. Steven R. Bagley

Computer Programming

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

C PROGRAMMING. Characters and Strings File Processing Exercise

PDS Class Test 2. Room Sections No of students

OBJECTIVE QUESTIONS: Choose the correct alternative:

C Programs: Simple Statements and Expressions

Input/Output Week 5:Lesson 16.1

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

Computer Programming: Skills & Concepts (CP) Strings

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

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

Appendix A Developing a C Program on the UNIX system

Chapter 21: Introduction to C Programming Language

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

Downloaded from

C Characters and Strings

Softprocessor SP3: Subprograms and Header

C programming basics T3-1 -

ALGORITHM 2-1 Solution for Exercise 4

Technical Questions. Q 1) What are the key features in C programming language?

Week 8 Lecture 3. Finishing up C

Tutorial No. 2 - Solution (Overview of C)

Transcription:

Unit 4 String String is an array of character. Strings and Library Functions A string variable is a variable declared as array of character. The general format of declaring string is: char string_name [size]; Here string_name is a variable name and size indicates your string contains maximum how many characters. When the compiler assigns the string to the character array, it automatically put the Null Character ( \0 ) at the end of string. Null character indicates the end of string. So the size of string should be maximum number of characters plus one. For example, in following case we have string name city and size of string is 10. char city[10]; Initialization of string variable can be done by two ways. One is a by simple method which we are using in initialization of array. char string_name[size] = { list of the character in string within single cot; For example, in following case string name has assigned value Hello. char name[10] = { H, e, l, l, o, \0 ; Another method is in which we assign the string directly within double cot. char string_name[size] = String ; For example, in following case we are assigning directly string Hello to variable name. char name[10] = Hello ; We can also initialize the string variable without specifying the size of variable. For example, char name[ ] = Hello ; Printing string To print the string we are using same printf function but we have to specify the output format as %s. printf( %s, string_name); For example, we have the following string variable name having value Hello World!. Then we are printing the string with printf function. char name[20] = Hello World! ; printf( %s\n, name); V.P. & R.P.T.P. Science College, V V Nagar Page 1

We can use formatting in printing string. In formatting we have to specify the total width (w) and number of characters we want to print from string (p). %w.ps For example, in above example of string Hello World! we want to specify the total width of print is 10 and from string we want to print string Hello then printf( %10.3s, name); We can also use puts( ) function to print the string. puts(string_name); Here there is no need to put the new line character after the string variable but function itself automatically put the new line after the string. For example, in above example if we use puts( ) function then output will be Hello World! with new line at end. puts(name); Reading String To read we can use scanf( ) function. To read string value we have to specify formatting character %s. But one thing in string reading, we have to not specify the address operator (&) in scanf( ) because string variable automatically determine its address. The format for the scanf( ) function is as under: scanf( %s, string_name); Before reading string variable by using scanf( ) function we have to clear the input buffer. For that in Turbo C/C++ we have to use fflush( ) function. fflush(stdin); // Clear standard input buffer There is one limitation of string reading by using simple scanf( ) function. It cannot read white space (spacebar, tab, new line). That means you cannot read entire string Hello World! by using simple function since it contains white space but we can read only Hello. Solution to read string variable with white space: 1. We have to read the entire string character by character in loop up to we is not entering new line character. At the end of string we have to explicitly put the null character to indicate the end of string. char c, name[100]; int i=0; do { c=getchar( ) ; // getchar() function read one character name[i] = c; i++; while (c!= \n ); name[i] = \0 ; 2. We have to use gets( ) function which read the string up to new line. char name[100]; gets(name); // this will read the string up to new line V.P. & R.P.T.P. Science College, V V Nagar Page 2

3. We can use same scanf( ) function with formatting to read the string. scanf( %[^\n], name); Here we have used %[^character] format means your computer read the string up to you are not entering the new line character. String Handling Function They are 1. strlen() 2. strcat() 3. strcpy() 4. strcmp() 5. strrev() To use these functions, the header file string.h must be included in the program using statement [1]. String Length: strlen() Syntax - int strlen() Description - It is used to find length of a given string. Length means total number of characters. Example - Write a program to find length of a string. #include<string.h> { char s1[50]; int len; clrscr(); printf( Enter your name ); scanf( %s,s1); len = strlen(s1) ; printf( \n Length = %d, len); getch(); Output: Enter your name Anjali Length = 6 [2]. String Copy: strcpy() Syntax - void strcpy(s2, s1) Where s1, s2 both are string. Description - It is used to copies the contents of one string to another. Here, the contents of s1 are copied into s2. s1 is source string and s2 is destination string. Example - The following program shows the use of strcpy() function. V.P. & R.P.T.P. Science College, V V Nagar Page 3

#include<string.h> { char s1[50], s2[50]; clrscr(); printf( Enter string ); scanf( %s,s1); strcpy(s2,s1) ; printf( \n Original string = %s & Copied string = %s, s1,s2); getch(); Output: Enter string Anjali Original string = Anjali & Copied string = Anjali [3]. String Concatenation: strcat() Syntax - void strcat(s1, s2) Where s1, s2 both are string. Description - It is used to append the contents of one string to another. Here, the contents of s2 are appended to s1. s1 is resultant string. Example #include<string.h> { char s1[50], s2[50]; clrscr(); printf( Enter string s1 ); scanf( %s,s1); printf( Enter string s2 ); scanf( %s,s2); strcat(s1,s2) ; printf( \n Resultant string = %s, s1); getch(); Output: Enter string s1 Tushar Enter string s2 Patel Resultant string = TusharPatel [4]. String Comparision: strcmp() Syntax - int strcmp(s1, s2) Where s1 and s2 are string. Description - It is used to compares two string. It returns 0 (zero) value if both s1 equal to s2, positive value if s1 greater than s2 and negative value if s1 less than s2. V.P. & R.P.T.P. Science College, V V Nagar Page 4

Example - #include<string.h> { int ans; char s1[50], s2[50]; clrscr(); printf( Enter string s1 ); scanf( %s,s1); printf( Enter string s2 ); scanf( %s,s2); ans = strcmp(s1,s2) ; if ( ans = = 0 ) printf( \n Both strings are equal ); if ( ans > 0 ) printf( \n String1 is greater ); if ( ans < 0 ) printf( \n String2 is gerater ); getch(); Output: Enter string s1 mamu Enter string s2 mamu Both strings are equal [5]. String reverse: strrev() Syntax - char *strrev(s1) Where s1 is string. Description - It is used to reverse the contents of a string. Example - #include<string.h> { char s1[50]; clrscr(); printf( Enter string ); scanf( %s,s1); printf( \n Original string = %s, s1); strrev(s1) ; printf( \n Reverse string = %s, s1); Output: Enter string xyz Original string = xyz Reverse string = zyx getch(); V.P. & R.P.T.P. Science College, V V Nagar Page 5

Common Standard Library Functions abs() Syntax - int abs(n) where n is an integer. Header file - math.h Description - It returns absolute value of an argument. Example - //Example on abs() function. #include<math.h> { int n = -123 ; printf( \n Given number : %d, n); printf( \n Absolute value : %d, abs(n)); Output - Given number : - 123 Absolute value : 123 pow() Syntax - double pow(a, b) where a and b are double. Header file - math.h Description - It returns value of a raised to b. Example - //Example on pow() function. #include<math.h> { float a = 2, b = 3 ; printf( \n 2 raised to 3 is : %0.2f, pow(a,b)); Output - 2 raised to 3 is 8.00 sqrt() Syntax - double sqrt(n) where n is double. Header file - math.h Description - It returns square root of an argument. Example - //Example on sqrt() function. V.P. & R.P.T.P. Science College, V V Nagar Page 6

#include<math.h> { float a = 25; printf( \n Square root of 25 is : %0.2f, sqrt(a)); Output - Square root of 25 is 5.00 toupper( ) Syntax - returnvalue = toupper(character) ; Description - This function is used to convert your character into uppercase from lowercase. Here the returnvalue should be character type. Example - char returnvalue; returnvalue = toupper( a ) ; Output the value of the returnvalue is A after execution. V.P. & R.P.T.P. Science College, V V Nagar Page 7

tolower( ) Syntax - returnvalue = tolower(character) ; Description - This function is used to convert your character into lower case from uppercase. Here the returnvalue should be character type. Example - char returnvalue; returnvalue = tolower( A ) ; Output Here the value of the returnvalue is a after execution. isupper() Syntax - int isupper( c ) where c is a character. Description - It returns true if an argument is an upper case letter (A to Z), otherwise false. Example - //Example on isupper() function. #include<ctype.h> { char c ; printf( Enter any character : ); scanf( %c,&c); if ( isupper(c) ) printf( It is an upper case letter. ); Output - Enter any character : A It is an upper case letter. getchar( ) Syntax - variablename = getchar( ) Header file - Stdio.h Description - This function is used to read simply one character from standard input device. Example - { char name ; printf( Enter name : ); name = getchar ( ) ; V.P. & R.P.T.P. Science College, V V Nagar Page 8

islower() Syntax - int islower( c ) where c is a character. Description - It returns true if an argument is a lower case letter (a to z), otherwise false. Example - //Example on islower() function. #include<ctype.h> { char c ; printf( Enter any character : ); scanf( %c,&c); if ( islower(c) ) printf( It is an lower case letter. ); Output - Enter any character : a It is an lower case letter. isapha() Syntax - int isalpha( c ) where c is a character. Description - It returns true if an argument is an upper case (A to Z) or lower case letter (a to z), otherwise false. Example - //Example on isalpha() function. #include<ctype.h> { char c ; printf( Enter any character : ); scanf( %c,&c); if ( isalpha(c) ) printf( It is an alphabets. ); Output - Enter any character : A It is an alphabets. V.P. & R.P.T.P. Science College, V V Nagar Page 9

isdigit() Syntax - int isdigit( c ) where c is a character. Description - It returns true if an argument is a digit, otherwise false. Example - #include<ctype.h> { char c ; printf( Enter any character : ); scanf( %c,&c); if ( isdigit(c) ) printf( It is a digit. ); Output - Enter any character : 5 It is a digit. getche( ) Syntax - variablename = getche( ) ; Header file - conio.h. Description - This function is used to read the character from the standard input device. Example - { char name ; printf( Enter name : ); name = getche( ) ; isalnum( ) Syntax - returnvalue = isalnum(character) ; Description - This function is used to check whether the character is digit (0 to 9) or alphabet (A to Z). Here the returnvalue should be integer type. If your character is digit or alphabet then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. Example - int returnvalue; returnvalue = isalnum( 3 ) ; Output Here the value of the returnvalue is non-zero since the 3 is digit. V.P. & R.P.T.P. Science College, V V Nagar Page 10

getch( ) Syntax - variablename = getch( ) Header file - conio.h. Description - This function is used to read the character from the standard input device but it will not echo (display) the character, which you are inputting. Example - { char name ; printf( Enter name : ); name = getch( ) ; putchar( ) Syntax - putchar(variablename) Header file - stdio.h Description - This function is used to print one character on standard output device. Example - { char name; name= P ; putchar(name); Output P ispunct( ) Syntax - returnvalue = ispunct(character) ; Description - This function is used to check whether the character is punctuation mark (special character. Not space, digit, alphabet, and nonprintable character). Here the returnvalue should be integer type. If your character is punctuation mark then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. Example - int returnvalue; returnvalue = ispunct( $ ) ; Output Here the value of the returnvalue is non-zero since the $ is punctuation mark. V.P. & R.P.T.P. Science College, V V Nagar Page 11

isspace( ) Syntax - returnvalue = isspace(character) ; Description - This function is used to check whether the character is space character (horizontal tab, new line, vertical tab, form feed, carriage return, space) or not. Here the returnvalue should be integer type. If your character is space character then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. Example - int returnvalue; returnvalue = isspace( ) ; Output Here the value of the returnvalue is non-zero since the is space character. isprint( ) Syntax - returnvalue = isprint(character) ; Description - This function is used to check whether the character is printable or not. Printable characters are alphabet (A to Z), digit (0 to 9), and special character ($, %, #, &, etc.) while control, alter, shift, bell, null character are non-printable character. Here the returnvalue should be integer type. If your character is printable then it will return true (non-zero value) otherwise it will return false (zero value). Here you can pass either the character variable or character constant as argument. Example - int returnvalue; returnvalue = isprint( 3 ) ; Output Here the value of the returnvalue is non-zero since the 3 is printable character. V.P. & R.P.T.P. Science College, V V Nagar Page 12