Lec 11.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

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.
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.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
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.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
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.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Chapter 9 Strings J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
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.
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.
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.
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.
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Characters and Strings
C Characters and Strings
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Arrays and Pointers CSE 2031 Fall September 2018.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Week 9 – Lesson 1 Arrays – Character Strings
McDonald’s Kalender 2009.
String What it is Why it’s useful
CprE 185: Intro to Problem Solving (using C)
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
2300 (11PM) September 21 Blue line is meridian..
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
McDonald’s calendar 2007.
Teacher name August phone: Enter text here.
Strings #include <stdio.h>
Arrays and Pointers (part 2)
CS31 Discussion 1H Fall18: week 6
Arrays and Pointers (part 2)
C Characters and Strings
McDonald’s calendar 2007.
Dr. Khizar Hayat Associate Prof. of Computer Science
2015 January February March April May June July August September
Computer Science II CS132/601* Lecture #C-3.
Introduction to Problem Solving and Programming
Presentation transcript:

Lec 11

What’s a String ? A string is a data structure that deals with the grouping of characters. In C, strings are implemented using arrays of type char.

Declaring and Initializing Strings Declaration char str_var[20]; /*declaring a string variable called str_var */ Initialization char str_var[20]={'A', 'p', 'p', 'l', 'e', ' ', 'a', 'n', 'd', ' ', 'P', 'e', 'a', 'r', 's'}; char str_var[20]="Apples and Pears";

Single and Double Quotes Single quotations are used to assign a character to a variable. Example: if s is declared as char s; then the assignment statement for s is s=‘L’; Double quotations are used to assign a string to a variable. Example: if s is declared as char s[10]; then the assignment statement for s is s="Computers";

Arrays of Strings Arrays of Strings is a two-dimensional array containing one character in each element. Example: char months[12][10]= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; char names[4][6]={"Sara", "Nora", "Nada", "Moudy"};

Definitions Null character The null character is represented by the character '\0'. This character marks the end of the string in C. White space characters Blanks (space bar), new lines (enter), and tabs. The %s placeholder Used in scanf’s and printf’s to print a string.

Initializers and Strings If number of initializers are less than the number of elements, the null character is added after the last character. char str_var[20]="Apples and Pears"; If number of initilaizers is more than the number of elements, a compiler error occurs. char x[4]="Hello"; If number of initilaizers equals the number of elements, a compiler overflow error occurs. char x[7]="Hangman";

scanf’s and Strings C o m p u t e r s \0 x scanf("%s", &x); /* word */ gets(x); /* word or sentince */ for (i=0; i<9; i++) scanf("%c", &x[i]); x[9]='\0'; Because the user cannot enter a null in a loop

scanf’s and Strings When scanf encounters whitespace characters before the string it skips these characters. When scanf encounters whitespace characters after the string it stops scanning and the null charcter is placed at the end.

printf’s and Strings C o m p u t e r s \0 x for (i=0; i<9; i++) printf("%c", x[i]); or for (i=0; x[i]!='\0'; i++) printf(" %c", x[i]); printf("%s", x);

Right and Left Justification Char x[7] = "Hello!"; Left Justification printf("%-20s", x); Right Justification printf("%20s", x); Hello! Hello!

String Manipulation Functions strcpy(x,y); strcpy: Makes a copy of the second argument into the first argument. It automatically adds the null to the end of the first argument. Examples: strcpy(x, "Hi"); strcpy(z, x); destination Source

String Manipulation Functions strncpy(x,y,n); strncpy: Makes a copy of up to n characters from the second argument to the first. It does not add the null. It can be added manually. Examples: strncpy(x, "Hi", 1); strncpy(z, x, 1);

Example /* a program to demonstrate string assignments */ #include<stdio.h> #include<string.h> int main(void) {char x[]="Happy Birthday to You"; char y[25], z[15]; printf("The string in array x is: %s\n", x); strcpy(y,x); printf("The string in array y is: %s\n", y); strncpy(z, y, 14); z[14]='\0'; printf("The string in array z is: %s\n", z); return(0);}

Example /* a program that divides a string into substrings */ #include<stdio.h> #include<string.h> int main(void) {char last[20], first[20], middle[20]; char name[20]="Lama Bandar Fahad"; strncpy(first, name, 4); first[4]='\0'; strncpy(middle, &name[5], 6); middle[6]='\0'; strcpy(last, &name[12]); printf("%s %s %s\n", last, first, middle); return(0);}

String Manipulation Functions strlen(x); strlen: Returns an integer number representing the number of characters in a string not including the null character. Examples: i=strlen("Hi"); 2 printf("%i", strlen("Hello")); 5

String Manipulation Functions strcat(x,y); strcat: Adds the string in the second argument to the end of the first. Examples: strcat(x, "!");

String Manipulation Functions strncat(x,y,n); strncat: Adds n characters of the string in the second argument to the end of the first one. Examples: strncat(x, "the end", 3);

String Manipulation Functions strcmp(x,y); strcmp: compares x and y. It returns a: 0 if x is equal to y. 1 if x is grater than y. -1 if x is less than y. Examples: #include<stdio.h> #include<string.h> int main(void) {char x[20]="universe"; char y[20]="university"; printf("%i\n", strcmp(x, y)); return(0);}

String Manipulation Functions Summary: strcpy(x,y); /* string assignment */ strncpy(x,y,n); strlen(x); /* determining string length */ strcat(x,y); /* appending to a string */ strncat(x,y,n); strcmp(x,y) /* comparing two strings */

Review (String Manipulation Functions) char x[10]="The", y[5]="End!"; int n=3; strcpy(x,y) strncpy(x,y,n) strlen(x) strcat(x,y) strncat(x,y,n) strcmp(x,y) x = "End!" x = "End" 3 x = "TheEnd!" x = "TheEnd" 1