String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.
 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 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
CS Nov 2006 C-strings.
Tutorial #8 Summer strings #include int main() { char str1[] = {‘h’,’e’,’l’,’l’,’o’}; char str[] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; char p[] = ”hello”;
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.
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
PRESENTED BY: ER. SUMANPREET KAUR LECTURER IN CE DEPTT. GPCG ASR.
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
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.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
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)
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Chapter 8: Character and String CMPD144: Programming 1.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
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
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
Strings, Slide Fundamental Programming Strings.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
ENEE150 – 0102 ANDREW GOFFIN Strings. Project 2 Flight Database 4 options:  Print flight  Print airport  Find non-stop flights  Find one-stop flights.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
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.
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
ECE Application Programming
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
String in C++.
EECE.2160 ECE Application Programming
C++ Pointers and Strings
Strings #include <stdio.h>
Characters and Strings Functions
EECE.2160 ECE Application Programming
C++ Pointers and Strings
Introduction to Problem Solving and Programming
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

String functions+ string I.Mona Alshehri

String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of a string. Return Value strlen returns the number of characters in s, not counting the null-terminating character. Example: #include #include #include void main(void) { char s[30]= "Borland International"; cout<<strlen(s); //print 21 getch(); }

String Functions: Header file:#include Function: strcpy(char s1[n],char s2[m]) Description Copies one string into another. Copies string s2to s1 stopping after the terminating null character has been moved. Return Value strcpy returns s1. Example: #include #include #include void main(void) { char string[10]; char str1[13] = "abcdefghi"; strcpy(string, str1); cout<<string; //print abcdefghi getch(); }

String Functions: Header file:#include Function: strcat(char dest[n], char src[m]); Description Appends one string to another. strcat appends a copy of src to the end of dest. The length of the resulting string is strlen(dest) + strlen(src). Return Value strcat returns a pointer to the concatenated strings. #include void main() { cout<<strcat("GOOD","MORNING"); getch(); } GOODMORNING Program Output

String Functions: Header file:#include strcmp() function It subtracts the ascii code of the first letter of the first string from the first letter of the second string, if the result is zero, it means that the letters are equal, and then it goes on to the second letters, whenever the result is non-zero, it means that the letters are not equal, so the comparison will stop. Example: Strcmp(COMPUTER,COMpUTER); 67-67=0 (Ascii code of C = 67) 79-79=0 (Ascii code of O = 79) 77-77=0 (Ascii code of M = 77) =-32 (Ascii code of P = 80 & Ascii code of p=112) STOP, The words are not equal.

String Functions: Example : #include void main() { char msg1[ ]="Hello"; char msg2[ ]="HELLO"; cout<<strcmp(msg1,msg2); cout<<strcmp(msg2,msg1); getch(); } Program Output

Example: Write a program that reads a line of text (string), and prints in table the number of capital letters and small letter in the input text? Examples: Enter Name: Salam it is a nice Home work Capital letter= 2 Small letter= 20

Solution: #include void main() { char word[20]; int capital=0,smal=0; cout<<"Enter the name:"; gets(word); for(int i=0;word[i]!=‘\0’;i++) if(word[i]>=' A‘ && word[i]<=‘Z’) capital++; if(word[i]>=‘a’ && word[i]<=‘z’) smal++; cout<<“Capital letter=“<<capital<<“\nSmall letter =“<<smal; getch(); }

Example: Write a program that reads a line of text (string), and prints in table the number of 3, 4, 5, and 6 letter words appearing in the text ? Examples: Text: Salam it is a nice home work Table : word length Number of words

Solution: #include void main() { char word[20]; int j=0,j3=0,j4=0,j5=0,j6=0; cout<<"Enter the sentance:"; gets(word); int len=strlen(word); for(int i=0;i<=len;i++) if(word[i]==' '|| i==len) switch(j) { case 3:{j3++;j=0;continue;} case 4:{j4++;j=0;continue;} case 5:{j5++;j=0;continue;} case 6:{j6++;j=0;continue;} } else j++; cout<<"Word lenth "<<"NO. of Word\n"; cout<<"3 "<<j3<<"\n"; cout<<"4 "<<j4<<"\n"; cout<<"5 "<<j5<<"\n"; cout<<"6 "<<j6<<"\n"; getch(); }