Introduction to Problem Solving and Programming

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
1 ICS103 Programming in C Lecture 17: Array of Strings.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
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.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
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.
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:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
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.
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.
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 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.
מערכים (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,
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
ICS103 Programming in C Lecture 15: Strings
Structure, Unions, typedef and enumeration
Lecture 8 String 1. Concept of strings String and pointers
Quiz 11/15/16 – C functions, arrays and strings
Lecture-5 Arrays.
A First Book of ANSI C Fourth Edition
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.
Programming and Data Structure
CS111 Computer Programming
C Stuff CS 2308.
Strings.
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
Week 9 – Lesson 1 Arrays – Character Strings
Strings A collection of characters taken as a set:
CprE 185: Intro to Problem Solving (using C)
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
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
CPS120: Introduction to Computer Science
Lec 11.
ICS103 Programming in C Lecture 15: Strings
Exercise Arrays.
ICS103 Programming in C Lecture 15: Strings
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
Arrays and Pointers (part 2)
Arrays and Pointers (part 2)
2015 January February March April May June July August September
Presentation transcript:

Introduction to Problem Solving and Programming Lecture 7 – Strings

Strings A string is any sequence of characters enclosed in double quotes “Hello World". There is no separate data type for strings as char, integer, float or double. Instead, a string is represented in C as an array of type char.

Strings We can declare and initialize a string variable using any of the following: char str1[20]={'H', 'e','l','l','o',' ','W','o','r','l','d','\0'}; //as other arrays char str2[20] = “Hello World"; /*with size specified explicitly */ char str3[]= “Hello World"; /*with size specified implicitly */ The string is terminated by the \0 character:

Strings For example, the declaration: char str[20] = “Hello World"; is actually represented in the memory as shown below: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 H e l o W r d \0

Input / Output of strings We can use %s to read and print a string printf (“Name is %s \n”, Name); scanf (“%s”, str); Pointer to character array

Input / Output of strings We can use %s to read and print a string printf (“Name is %s \n”, Name); scanf (“%s”, str); Where is &

Input / Output with gets and puts A problem with scanf when reading a string is that it stops scanning the moment it encounters a white space. Thus, it cannot scan a string such as: “Ali Zaki Street” in one variable. An alternative to scanf is the function gets that takes a string variable as argument. char school[SIZE]; gets(school);

Input / Output with gets and puts A problem with scanf when reading a string is that it stops scanning the moment it encounters a white space. Thus, it cannot scan a string such as: “Ali Zaki Street” in one variable. An alternative to scanf is the function gets that takes a string variable as argument. char school[SIZE]; gets(school); No &

Input / Output with gets and puts The gets function continues to scan for characters until it encounters the new line character – until the user types the Enter key. The function puts can be used to print a string. puts(school);

String.h To use some operations or functions on strings, we need to include file string.h #include<string.h> So, we can use functions strcpy(s1, s2) strlen(s) strcmp(s1, s2) strcat (s1, s2)

String Functions strcpy(s1, s2) we can not use operator “ = “ with string strcpy is copying s2 into s1 (s1  s2)

Strcpy Array style void strcpy(char dest[], char src[]) { int i = 0; while (src[i] != ‘\0’) { dest[i] = src[i]; i++; } dest[i] = ‘\0’

Strcpy Pointer style void strcpy(char* dest, char* src) { while (*src != ‘\0’) { *dest = *src; dest++; src++; }

String Functions strlen(S) Return the length of string S strcpy(S, “industrial robot”); strlen(S) will return 16 Note the difference between this and: char s[]=“industrial robot” ;

Strlen Array style int strlen(char str[]) { int i = 0; while (str[i] != ‘\0’) { i++; } return i;

Strlen Pointer style int strlen(char* str) { int i = 0; while (*str != ‘\0’) { i++; str++; } return i;

String Functions strcmp(s1, s2) It is for comparison of s1 and s2 It return 0 if s1=s2 Return –ve if s1 is less than s2 Return +ve if s1 is greater than s2

String Functions It appends/concatenates s2 to the end of s1 strcat(s1, s2) It appends/concatenates s2 to the end of s1 Example: s1= “Lion” , s2 = “King” strcat(s1,s2)  s1=“LionKing”

Example (1) #include <stdio.h> #include <string.h> int main() { int k; char src [30]; char dest [10]=" Tutorial"; strcpy(src, "This is a"); strcat(src, dest); k=strlen(src); printf("Final string : %s, with length = %d\n", src,k); return(0); }

Example (2) /* Compare two strings and determine which is greater*/ gets(s); gets(d); k=strcmp(s,d); if (k<0) printf("\n First is less than second \n"); else if (k==0) printf("\n First equals second \n"); else if (k>0) printf("\n First is greater than second \n");

Example (3) Write a function to reverse a string. Write a program to check if a given string is a “palindrome”. Discuss alternative approaches, one of which you can use the reverse function.

Array of Strings To represent arrays of strings we need 2- dimensional arrays of characters. The first-dimension represents the number of strings in the array and the second-dimension represents the strings. The following are statements to declare an array to store up to 30 names, each of maximum length, 25 characters. #define NUM_NAMES 30 #define NAME_LEN 25 ... char names[NUM_NAMES][NAME_LEN];

Array of Strings We can also initialize an array of strings at declaration in the following manner: char month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

Example // Print array of strings #include <stdio.h> int main() { int i; char Names[3][20]={"Akram Ali“ , “Mido Zaki", , ”Said Abdo”}; for (i=0; i<3; i++) printf("%s\n",Names[i]); return(0);}

Array of Pointers to strings For example: an array of strings char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; Strings are pointers to the first character Each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit array has a fixed size, but strings can be of any size

Arrays of Pointers to strings char * – each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’\0’ ’D’ ’i’ ’m’ ’o’ ’n’ ’d’ ’C’ ’l’ ’u’ ’b’ ’S’ ’p’

Example (Array of strings) char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; int main() { int x ; for (x = 0; x < 4 ; x++) printf("\n %s --- %d ", suit[x],strlen(suit[x])); return 0; }  

Questions?