Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to C Programming
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 10.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer Programming for Engineers
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Computer Organization and Design Pointers, Arrays and Strings in C
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
UNIT 5 C Pointers.
ECE Application Programming
Fundamentals of Characters and Strings
Computer Programming BCT 1113
Lecture 8 String 1. Concept of strings String and pointers
Quiz 11/15/16 – C functions, arrays and strings
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Arrays in C.
Computer Science 210 Computer Organization
CS111 Computer Programming
CS1100 Computational Engineering
Strings.
INC 161 , CPE 100 Computer Programming
Week 9 – Lesson 1 Arrays – Character Strings
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Chapter 2 Array and String Visit to more Learning Resources.
EECE.2160 ECE Application Programming
Chapter 16 Pointers and Arrays
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
C Strings Prabhat Kumar Padhy
Exercise Arrays.
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
CS31 Discussion 1H Fall18: week 6
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Introduction to Problem Solving and Programming
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area Hrs 4 Strings 2 Character Arrays and Strings, String Manipulation KIIT UNIVERSITY

What are Strings KIIT UNIVERSITY Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character i.e. ‘\0’, which is a character all of whose bits are zero. A string constant is a one-dimensional array of characters. For example, char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ; Each character in the array occupies one byte of memory and the last character is always ‘\0’. What character is this? It looks like two characters, but it is actually only one character, with the \indicating that what follows it is something special. ‘\0’ is called null character. Note that ‘\0’ and ‘0’ are not same. ASCII value of ‘\0’ is 0, whereas ASCII value of ‘0’ is 48. KIIT UNIVERSITY

Printing of a string KIIT UNIVERSITY Code 1 Code 2 Code 3 The terminating null (‘\0’) is important, because it is the only way the functions that work with a string can know where the string ends. In fact, a string not terminated by a ‘\0’ is not really a string, but merely a collection of characters. C concedes the fact that you would use strings very often and hence provides a shortcut for initializing strings. For example, the string used above can also be initialized as, char name[ ] = "HAESLER" ; Note that, in this declaration ‘\0’ is not necessary. C inserts the null character automatically. Code 1 Code 2 Code 3 char name[ ] = "Klinsman" ; int i = 0 ; while ( i <= 7 ) { printf ( "%c", name[i] ) ; i++ ; } char name[ ] = "Klinsman" ; int i = 0 ; while ( name[i] != `\0' ) { printf ( "%c", name[i] ) ; i++ ; } char name[ ] = "Klinsman" ; char *ptr ; ptr = name ; /* store base address of string */ while ( *ptr != `\0' ) { printf ( "%c", *ptr ) ; ptr++ ; } KIIT UNIVERSITY

Printing of a string KIIT UNIVERSITY There are so many ways (as shown in previous slide) to refer to the elements of a character array, rarely is any one of them used. This is because printf( ) function has got a sweet and simple way of doing it, as shown below. Note that printf( ) doesn’t print the ‘\0’. int main( ) { char name[ ] = "Klinsman" ; printf ( "%s", name ) ; return 0; } The %s used in printf( ) is a format specification for printing out a string. The same specification can be used to receive a string from the keyboard, as shown below. char name[25] ; printf ( "Enter your name " ) ; scanf ( "%s", name ) ; printf ( "Hello %s!", name ) ; KIIT UNIVERSITY

Entering the string using scanf( ) While entering the string using scanf( ) we must be cautious about two things: The length of the string should not exceed the dimension of the character array. This is because the C compiler doesn’t perform bounds checking on character arrays. Hence, if you carelessly exceed the bounds there is always a danger of overwriting something important, and in that event, you would have nobody to blame but yourselves. scanf( ) is not capable of receiving multi-word strings. Therefore names such as ‘Hrithik Roshan’ would be unacceptable. The way to get around this limitation is by using the function gets( ). The usage of functions gets( ) and its counterpart puts( ) is shown below. char name[25] ; printf ( "Enter your full name " ) ; gets ( name ) ; puts ( name ) ; KIIT UNIVERSITY

Standard Library String Function KIIT UNIVERSITY

Standard Library String Function KIIT UNIVERSITY

strlen() KIIT UNIVERSITY Output Declaration - size_t strlen(const char *str) where size_t is the unsigned integral type and str is the base address of the string Note - Computes the length of the string up to but not including the terminating null character. int main( ) { char arr[ ] = "Bamboozled" ; int len1, len2 ; len1 = strlen ( arr ) ; len2 = strlen ( "Humpty Dumpty" ) ; printf ( "\nstring = %s length = %d", arr, len1 ) ; printf ( "\nstring = %s length = %d", "Humpty Dumpty", len2 ) ; return 0; } Output string = Bamboozled length = 10 string = Humpty Dumpty length = 13 KIIT UNIVERSITY

strcpy() KIIT UNIVERSITY Output Declaration - char *strcpy(char *dest, const char *src) where dest is the pointer to the destination array where the content is to be copied and src is the string to be copied and the function returns a pointer to the destination string dest. int main( ) { char source[ ] = "Sayonara" ; char target[20] ; strcpy ( target, source ) ; printf ( "\nsource string = %s", source ) ; printf ( "\ntarget string = %s", target ) ; return 0; } Output source string = Sayonara target string = Sayonara KIIT UNIVERSITY

strcat() KIIT UNIVERSITY Output Declaration - char *strcat(char *dest, const char *src) where dest is the pointer to the destination array where the content is to be copied and should be large enough to contain the concatenated resulting string. src is the string to be copied and the function returns a pointer to the destination string dest. int main( ) { char source[ ] = "Students!" ; char target[30] = "Hello" ; strcat ( target, source ) ; printf ( "\nsource string = %s", source ) ; printf ( "\ntarget string = %s", target ) ; return 0; } Output source string = Students! target string = HelloStudents! KIIT UNIVERSITY

strcmp() KIIT UNIVERSITY Output Declaration - int strcmp(const char *str1, const char *str2). str1 is the first string to be compared and str2 is the second string to be compared. Return Value - This function return values that are as follows: if return value < 0 then it indicates str1 is less than str2. if return value > 0 then it indicates str2 is less than str1. if return value = 0 then it indicates str1 is equal to str2. int main( ) { char string1[ ] = "Jerry" ; char string2[ ] = "Ferry" ; int i, j, k ; i = strcmp ( string1, "Jerry" ) ; j = strcmp ( string1, string2 ) ; k = strcmp ( string1, "Jerry boy" ) ; printf ( "\n%d,%d,%d", i, j, k ) ; return 0; } Output 0,4,-32 KIIT UNIVERSITY

Two-Dimensional Array of Characters #define FOUND 1 #define NOTFOUND 0 int main( ) { char masterlist[6][10] = { "akshay", "parag", "raman", "srinivas", "gopal", "rajesh" } ; int i, flag, a ; char yourname[10] ; printf ( "\nEnter your name " ) ; scanf ( "%s", yourname ) ; flag = NOTFOUND ; for ( i = 0 ; i <= 5 ; i++ ) a = strcmp ( &masterlist[i][0], yourname ) ; if ( a == 0 ) printf ( "Welcome, you can enter the palace" ) ; flag = FOUND ; break ; } if ( flag == NOTFOUND ) printf ( "Sorry, you are a trespasser" ) ; return 0; Memory Map KIIT UNIVERSITY

Passing Strings to Functions String can be passed to function in similar manner as arrays as, string is also an array. #include <stdio.h> #include <string.h> void display(char ch[]); int main() { char c[50]; printf("Enter string: "); gets(c); display(c); // Passing string to function. return 0; } void display(char ch[]) printf("String Output: "); puts(ch); KIIT UNIVERSITY

Class Work (CW) KIIT UNIVERSITY WAP to convert the string from lower case to upper case WAP to convert the string from upper case to lower case WAP to reverse the string KIIT UNIVERSITY

Thank You KIIT UNIVERSITY

Home Work (HW) KIIT UNIVERSITY WAP that extracts part of the given string from the specified position. For example, if the sting is "Working with strings is fun", then if from position 4, 4 characters are to be extracted then the program should return string as "king". Moreover, if the position from where the string is to be extracted is given and the number of characters to be extracted is 0 then the program should extract entire string from the specified position. WAP that replaces two or more consecutive blanks in a string by a single blank. For example, if the input is Grim return to the planet of apes!! the output should be Grim return to the planet of apes!! WAP to sort a set of names stored in an array in alphabetical order. WAP to delete all vowels from a sentence. Assume that the sentence is not more than 80 characters long. WAP to check whether the given string is palindrome. A palindrome is a word or a number or a sequence of words that can be read the same way from either direction, be it forwards or backwards. E.g. EYE, RACECAR or MADAM are palindrome. WAP to remove characters in string except alphabets KIIT UNIVERSITY

Home Work (HW) WAP that receives the month and year from the keyboard as integers and prints the calendar in the following Note that according to the Gregorian calendar 01/01/1900 was Monday. With this as the base the calendar should be generated. KIIT UNIVERSITY