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

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

ONE DIMENSIONAL ARRAYS

Strings and Library Functions

Introduction to string

Multiple Choice Questions ( 1 mark)

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

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

Chapter 8 Character Arrays and Strings

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

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.

Computers Programming Course 11. Iulian Năstac

Computers Programming Course 10. Iulian Năstac

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

ARRAYS(II Unit Part II)

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

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

C Characters and Strings

today cs3157-fall2002-sklar-lect05 1

Strings. Daily Puzzle

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

Fundamentals of Programming

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

Fundamental of Programming (C)

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 1 Getting Started Structured Programming 1

LAB7 : Characters and Strings

by Pearson Education, Inc. All Rights Reserved.

Euclid s algorithm, 133

Computers Programming Course 5. Iulian Năstac

Computers Programming Course 12. Iulian Năstac

Chapter 8 C Characters and Strings

C mini reference. 5 Binary numbers 12

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

Arrays, Strings, & Pointers

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

8. Characters, Strings and Files

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

Chapter 21: Introduction to C Programming Language

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

Array Initialization

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

Number Systems, Scalar Types, and Input and Output

INDORE INDIRA SCHOOL OF CAREER STUDIES C LANGUAGE Class B.Sc. - IIND Sem

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

C programming basics T3-1 -

BSM540 Basics of C Language

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

Computer Programming Unit v

CSC 2400: Computer Systems. Arrays and Strings in C

Functions: call by reference vs. call by value

Intermediate Programming, Spring 2017*

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

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

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

THE FUNDAMENTAL DATA TYPES

M.CS201 Programming language

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

Programming and Data Structures

CSC 2400: Computer Systems. Arrays and Strings in C

Chapter 8 - Characters and Strings

Lesson 7. Reading and Writing a.k.a. Input and Output

Course organization. Course introduction ( Week 1)

Engineering Problem Solving with C++, Etter

2. Numbers In, Numbers Out

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

Scientific Programming in C V. Strings

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

UNIT III ARRAYS AND STRINGS

C Programming Language Review and Dissection III

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

2. Numbers In, Numbers Out

Characters and Strings

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Full file at C How to Program, 6/e Multiple Choice Test Bank

COMP1917: 09 Arrays and Strings

9/9/2017. Prof. Vinod Mahajan

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

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

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

Fundamentals of Programming & Procedural Programming

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

Chapter 7. Basic Types

C Programming Multiple. Choice

Create a Program in C (Last Class)

Week 8 Lecture 3. Finishing up C

BİL200 TUTORIAL-EXERCISES Objective:

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

BITG 1113: Array (Part 2) LECTURE 9

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

printf( Please enter another number: ); scanf( %d, &num2);

IPC144 - Introduction to Strings. array of characters is an array, each element of which has a character data type.

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

UNIT-I Input/ Output functions and other library functions

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

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

Q 1. Attempt any TEN of the following:

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

Transcription:

Strings

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters, decimal digits, special characters and escape sequences. A character is usually stored in the computer as an 8-bits (1 byte) integer. The integer value stored for a character depends on the character set used by the computer on which the program is running. 2

There are two commonly used character sets: ASCII (American Standard Code for Information Interchange) EBCDIC (Extended Binary Coded Decimal Interchange Code) 3

Difference Between an Integer Digit and a Character Digit char num = 1 and char num = 1 are not the same. char num = 1 is represented in the computer as 00000001. char num = 1 on the other hand is number 49 according to the ASCII character set. Therefore, it is represented in the computer as 00110001. 4

#include <stdio.h> void main(void) { char my_a = 'A'; char my_z = 'Z'; char my_a = 'a'; char my_z = 'z'; printf("\nascii value for A is %d", my_a); printf("\nascii value for Z is %d",my_z); printf("\nascii value for a is %d", my_a); printf("\nascii value for z is %d",my_z); } printf("\n"); printf("\n65 in ASCII represents %c",65); printf("\n90 in ASCII represents %c",90); printf("\n97 in ASCII represents %c",97); printf("\n122 in ASCII represents %c",122); 5

ASCII value for A is 65 ASCII value for Z is 90 ASCII value for a is 97 ASCII value for z is 122 65 in ASCII represents A 90 in ASCII represents Z 97 in ASCII represents a 122 in ASCII represents z 6

#include <stdio.h> void main(void) { char ch; } printf("enter a character: "); scanf("%c", &ch); if (ch >= 'A' && ch <= 'Z') { printf("\ncapital letter\n"); } #include <stdio.h> void main(void) { char ch; } printf("enter a character: "); scanf("%c", &ch); if (ch >= 65 && ch <= (65+26)) { printf("\ncapital letter\n"); } equivalent to 7

A string in C is an array of characters ending with the null character ( \0 ). It is written inside a double quotation mark ( ) A string may be assigned (in a declaration) to either a char array or to a char pointer: char color[] = green ; OR char *color = green ; 8

A string can also be defined by specifying the individual characters: char color[ ] = { g, r, e, e, n, \0 }; A string is accessed via a pointer to the first character in the string. In memory, these are the characters stored: g r e e n \0 9

Notice that even though there are only five characters in the word green, six characters are stored in the computer. The last character, the character \0, is the NULL character which indicates the end of the string. Therefore, if an array of characters is to be used to store a string, the array must be large enough to store the string and its terminating NULL character. 10

We can initialize string variables at compile time such as; char name[10] = Arris ; This initialization creates the following spaces in storage : A r r i s \0 \0 \0 \0 \0 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 11

If we happen to declare a string like this: char my_drink[3] = tea ; We will get the following syntax error: error C2117: 'tea' : array bounds overflow Instead, we need to at least declare the array with (the size of the string + 1) to accommodate the null terminating character \0. char my_drink[4] = tea ; 12

#include <stdio.h> void main(void) /* a program that counts the number of characters in a string */ { char sentence[] = "I love C language"; int i, count = 0; for (i = 0; sentence[i]!= '\0'; i++) { count++; } } printf( %s has %d characters including the whitespace", sentence, count); Sample output: I love C language has 17 characters including the whitespace 13

Standard Functions Input scanf( ) gets( ) Standard Functions Output printf( ) puts( ) Use scanf function together with the format specifier %s for interactive input string. (no whitespace character) If the string to be read as an input has embedded whitespace characters, use standard gets function. 14

String : A string is a collection of characters. Strings are always enclosed in double quotes as "string constant". Strings are used in string handling operations such as, Counting the length of a string. Comparing two strings. Copying one string to another. Converting lower case string to upper case. Converting upper case string to lower case. Joining two strings. Reversing string.

Declaration : The string can be declared as follow : Syntax: char string_nm[size]; Example: char name[50];

String Structure : When compiler assigns string to character array then it automatically supplies null character ('\0') at the end of string. Thus, size of string = original length of string + 1. char name[7]; name = "TECHNO"

Read Strings : To read a string, we can use scanf() function with format specifier %s. char name[50]; scanf("%s",name); The above format allows to accept only string which does not have any blank space, tab, new line. Write Strings : To write a string, we can use printf() function with format specifier %s. char name[50]; scanf("%s",name); printf("%s",name);

string.h header file : 'string.h' is a header file which includes the declarations, functions, constants of string handling utilities. These string functions are widely used today by many programmers to deal with string operations. Some of the standard member functions of string.h header files are, Function Name Description strlen - Returns the length of a string. strlwr - Returns upper case letter to lower case. strupr - Returns lower case letter to upper case. strcat - Concatenates two string. strcmp - Compares two strings. strrev - Returns length of a string. strcpy - Copies a string from source to destination.

Program : /* Program to demonstrate string.h header file working. #include <stdio.h> #include <conio.h> #include <string.h> void main() { char str[50]; clrscr(); printf("\n\t Enter your name : "); gets(str); printf("\nlower case of string: %s",strlwr(str)); printf("\nupper case of string: %s",strupr(str)); } Output : printf("\nreverse of string: %s",strrev(str)); printf("\nlength of String: %d",strlen(str)); getch(); Enter your name : Technoexam Lower case of string: technoexam Upper case of string: TECHNOEXAM Reverse of string: MAXEONHCET Length of String: 10_