CS111 Computer Programming

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
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.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
מערכים (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.
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.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
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.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Chapter 8 “Character Arrays and Strings” Prepared by: Prof. Ajay M. Patel CE, IDS, NU.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Strings (Continued) Chapter 13
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
CSE 303 Lecture 14 Strings in C
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Programming Languages and Paradigms
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSI 121 Structured Programming Language Lecture 21 Character Strings
INC 161 , CPE 100 Computer Programming
Pointers and Pointer-Based Strings
Strings A collection of characters taken as a set:
Lecture 11 Strings.
String in C++.
Chapter 2 Array and String Visit to more Learning Resources.
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
Exercise Arrays.
Chapter 9: Pointers and String
C++ Programming Lecture 20 Strings
Strings in C Array of characters is called a string.
Lecture 19: Working with strings
Strings #include <stdio.h>
Programming Languages and Paradigms
CS1100 Computational Engineering
Introduction to Problem Solving and Programming
Presentation transcript:

CS111 Computer Programming strings

Strings A string is a sequence of characters treated as a group We have already used some string literals: “filename” “output string” Strings are important in many programming contexts: names other objects (numbers, identifiers, etc.)

Declaration char name[16]; No explicit type, instead strings are maintained as arrays of characters Representing strings in C stored in arrays of characters array can be of any length end of string is indicated by a delimiter, the zero character ‘\0’ char name[16]; Declares a char name of maximum length 15 characters (elements 0 to 14, element 15 stores the null character) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \0

Initialization Like other arrays it is possible to do assignment

‘c’ vs “c” At first sight, a string consisting of a single character might be thought to be identical to a char variable. However this is not the case. “c” c \0 ‘c’ c

Taking Input from Keyboard scanf can be used to take input a string from the keyboard Format specifier for a string is %s Example: char address[15]; scanf(“%s”,address); Issue: Space is a delimiter Scanf terminates its input on the first white space

Taking Input from Keyboard How to read a line of text? Alternatives? Repeatedly check for the user input Use standard library function Input a character Assign to the array of character Increment the Index Check if the character was ‘\n’ If not, then go to step 1 Go one index back in the array Assign ‘\0’ to that location Any issue in this? 

Repeatedly check for the user input Input a character Assign to the array of character Increment the Index Check if the character was ‘\n’ If not, then go to step 1 Go one index back in the array Assign ‘\0’ to that location  scanf() … ? A new function int getchar(void); Reads the next character from stdin.

#include<stdio.h> int main() { char myArr[20],ch; int ind = 0; do { ch = getchar(); myArr[ind] = ch; ind++; }while(ch!='\n'); myArr[ind-1] = '\0'; …

Not so common approach int main() { char name[20]; printf("\nEnter your name: "); scanf("%[^\n]",name); printf("\n Welcome %s",name); }

Accessing string elements It is a character array Stored in contiguous memory location 1 2 3 4 5 6 7 8 9 10 11 I T J O D H P U R \0 ind = 0; while(ind<11){ … ind++; } ind = 0; while(myArr[ind]!=‘\0’){ … ind++; }

Library Functions To get input from the user gets() To print string on the screen puts()

String and pointer char name[] = {‘I’, ‘I’, ‘T’, ‘J’, ‘O’, ‘D’, ‘H’, ‘P’, ‘U’, ‘R’, ‘\0’}; I T J O D H P U R \0 int i=0; while(name[i]){ printf(“\n %c %c %c %c”, name[i], *(name+i), *(i+name), i[name]); }

String variable (Pointer) … char name[] = “IIT Jodhpur”; char *myName = name; while( *myPtr != ‘\0’) { printf(“\n %c”, *ptr); ptr++; }

Standard Library Functions Calculate the length of a string : strlen() Copy a string to another string : strcpy() Concatenate two strings : strcat() Compare a pair of strings: strcmp()

Example str1 I T \0 J O D H P U R str2 str3 I T J O D H P U R \0 I T \0 strlen(str1) = 3; strlen(str2) = 7; strlen(str3) = 0; strcpy(str3, str1) strcat(str3, str2)

1 -1 STRCMP(STR1,STR2) str1 J A I P U R \0 str2 str1 J A I P U R \0 G str2 1 str1 J A I P U R \0 G str2 str1 -1 J A I P U R \0 G str2

Pointers and Strings We can not assign one string to another string We can assign one character pointer to another

Pointers and Strings Once a string is defined it can’t be reinitialized Such operations are perfectly fine with pointers

Constant Pointer and String Variable Pointer, Variable String Variable Pointer, Constant String Constant Pointer, Variable String Constant Pointer, Constant String

char cfti[6][10]= { } 2D Array of Characters M A D R S \0 K H G P U B W T I N E }

Assign value through Keyboard … char cfti[6][10]; for (i=0; i<6; i++) scanf(“%s”, &cfti[i][0]);

Waste of memory !! Memory allocation M A D R S \0 K H G P U B O Y W T

Array of pointers to strings char *cfti[6]= { M A D R S \0 K H G P U B O Y W T I N E }

Memory Allocation } char *cfti[6]={ K H A R G P U \0 K A N P U R \0 B W A H T I \0 M A D R S \0 R O K E Y \0

Limitation of Array of Pointers to Strings In 2D char array we can either initialize at time of declaration or take input through scanf() In case of array of pointers to strings: We can not take input through keyboard

Solution … …