Introduction to C Programming CE00312-1 Lecture 13 Strings in C.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
Introduction to C Programming CE
CS 201 String Debzani Deb.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
Introduction to C Programming CE Lecture 14 Strings and Searching.
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
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.
1 بنام خدا زبان برنامه نویسی C (21814( Lecture 13 Chapter 13 Strings.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
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)
Introduction to Programming 3D Applications CE Lecture 12 Structure Pointers and Memory Management in C.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;
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.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 6: Arrays 数据序列, 数组 1.
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Introduction to Programming 3D Applications CE Lecture 10 Pointers in C.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
Arrays and Pointers.
Strings, Pointers and Tools
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
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.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Strings CSCI 112: Programming in C.
Lecture 8 String 1. Concept of strings String and pointers
Arrays in C.
INC 161 , CPE 100 Computer Programming
Lecture 11 Strings.
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
EECE.2160 ECE Application Programming
Arrays.
Strings in C Array of characters is called a string.
Introduction to Problem Solving and Programming
Presentation transcript:

Introduction to C Programming CE Lecture 13 Strings in C

A string is an array of characters terminated by a ‘\0’ character and is limited to the size of the array. e.g. char name[10]; can represent a string of maximum 9 characters (10 including ‘\0’) Initialisation: char word[15] = “example”; // ‘\0’ in addition, rest undefined

An array name or string name is a reference to an array of elements (called a pointer in C). Using a string as an entity (ie with a ‘\0’), as opposed to an array of individual characters, allows us to use the string facilities of C without thinking about the ‘\0’

Input and Output of Strings Output: use s format in printf e.g. printf(“%s”, word); Input: Two forms: 1)s format can be used in scanf egscanf(“%s”, name); // no ‘&’ required for a string reads all characters up to space or newline.

Input 2)gets reads a whole line as a string and then we can extract the data in the line individually using sscanf (note the extra ‘s’). gets returns NULL if there is no more data in the input (for the keyboard when control and ‘d’ are pressed together).

Example of using gets char line[81]; // maximum 80 chars + ‘\0’ char name[21]; int age; gets(line);// stores all characters // replaces ‘\n’ by ‘\0’ sscanf(line, “%s%d”, name, &age); scans the string, line, stores a string (terminated by a space) in name, and an integer in age, thus for the following input: Bailey 60 string name contains “Bailey” and age contains 60. Subsequent reading starts on the next line.

Standard String Functions #include “string.h” // required for using string functions strlen(s)// returns length of string, s - // not counting ‘\0’ For example: printf(“string %s is of length %d”, word, strlen(word));

More String Functions strcpy(s1, s2)// copies string s2 to s1, // but s1 = s2 is incorrect in C For example strcpy(word, name); strcat(s1,s2)// appends s2 on to end of s1, // but s1 must be big enough For example strcat(word, name); // word now contains “exampleBailey”

Note: strcpy and strcat also return s1 For example printf(“concatenation of %s and %s is %s”, word, name, strcat(word, name)); produces concatenation of example and Bailey is exampleBailey

Comparing Strings strcmp(s1, s2) // returns value of 0 if s1 the same as s2, // returns -ve value if s1 less than s2, // returns +ve value if s1more than s2, // we can’t use ==, For example if (strcmp(name, word) < 0) { printf(“%s is less than %s”, name, word); }

String example No. 1 Input 10 strings (each of at most 20 characters) into an array, sort them into alphabetical order and output them. #include “stdio.h” #include “string.h” // prototypes void read_strings(char [][21], int); void print_strings(char [][21], int); void sort(char [][21], int);

Main Function Array of 10 strings, list, must be declared in main. Array name, list, and its size, 10, must be passed to each function that uses it. int main(void) { char list[10][21]; // 20 chars each + ‘\0’ read_strings(list, 10); print_strings(list, 10); sort(list, 10); print_strings(list, 10); return 0; }// end main

read_strings function Array, s, is 2-dimensional array (only no. of columns, ie 21, declared) - each row is a string. Function, gets, reads a line (ie string) into row s[i]. void read_strings(char s[][21], int n) { int i; printf(“\ntype one string per line\n”); for (i=0; i<n; i++) {// read each line into row s[i] gets(s[i]); } }// end read strings

print_strings function Print each row of array, list, as a string using s format. void print_strings(char list[][21], int n) { int i; printf(“\nlist of strings is:\n\n”); for (i=0; i<n; i++) {// print each row list[i] as string printf(“%s\n”, list[i]); } }// end print strings

void sort(char a[][21], int n) {// Array, a, of strings int i, j; char temp[21];// temp string for (j = n-1; j > 0; j--) {// each pass of j comparisons for (i = 0; i < j; i++) {// each comparison if (strcmp(a[i], a[i+1]) > 0) // a[i] > a[i+1] {// swap the 2 strings strcpy(temp, a[i]); strcpy(a[i], a[i+1]); strcpy(a[i+1], temp); }// else no swap }// end of each pass }// end of all passes }// end sort