Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout << length;

Slides:



Advertisements
Similar presentations
Unit 10 Miscellaneous Advanced Topics Introduction to C Programming.
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
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.
EEE 243B Applied Computer Programming Strings. Example #1 Write code to take a first name and last name and create a new string with the full name first.
 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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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'; }
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 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
Character Arrays strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */ char pmessage[] = "now.
Characters and Strings 4 Character constants (single quotes): –‘a’, ‘A’, ‘0’, ‘1’, ‘\n’, ‘\0’, … 4 String constants (double quotes): –“Mary had a little.
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”;
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
C-strings Array with base type char One character per indexed variable
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.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Introduction to C programming
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
February 14, 2005 Characters, Strings and the String Class.
Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string,
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CSC 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why.
Strings in C are my friend, and they can be yours, too. An inspiring and epic tale by Erik Speyer, John Sullivan, and Dane Bennington.
COP 3530 Data Structures & Algorithms Discussion Session 3.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Duke CPS 6. 1 arrays and strings: pointers and memory allocation l Why not rely solely on string and Vector classes? ä how are string and Vector implemented?
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
Sahar Mosleh California State University San MarcosPage 1 Character String.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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];
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
C vs. C++ I/O Declaration Placement Strings Minor syntax differences (ala enum)
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.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
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.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Fundamentals of Characters and Strings
Null-Terminated Character Arrays
INC 161 , CPE 100 Computer Programming
Strings #include <stdio.h>
Characters and Strings Functions
Presentation transcript:

Built-in Functions for NTCAs

strlen char array[10] = “Hello”; int length = strlen(array); cout << length;

strlen char array[10] = “Hello”; int length = strlen(array); cout << length; hello\0???? [0][1][2][3][4][5][6][7][8][9]

strlen char array[10] = “Hello”; int length = strlen(array); cout << length; hello\0???? [0][1][2][3][4][5][6][7][8][9]

5

strcpy char source[10] = “Hey”; char target[10] = “Boy Howdy”; strcpy(target, source);

strcpy char source[10] = “Hey”; char target[10] = “Boy Howdy”; strcpy(target, source); hey\0?????? [0][1][2][3][4][5][6][7][8][9] source:

strcpy char source[10] = “Hey”; char target[10] = “Boy Howdy”; strcpy(target, source); hey\0?????? [0][1][2][3][4][5][6][7][8][9] source: Boy Howdy\0 [0][1][2][3][4][5][6][7][8][9] target:

strcpy char source[10] = “Hey”; char target[10] = “Boy Howdy”; strcpy(target, source); hey\0?????? [0][1][2][3][4][5][6][7][8][9] source: hey\0Howdy [0][1][2][3][4][5][6][7][8][9] target:

strcpy... strcpy(target, “Hi”); strcpy(target, “BurritoToppings”; Hi\0 Howdy [0][1][2][3][4][5][6][7][8][9] target:

strcpy... strcpy(target, “Hi”); strcpy(target, “BurritoToppings”; BurritoTop [0][1][2][3][4][5][6][7][8][9] target:

strcat char source[10] = “There”; char target[10] = “Hi”; strcat(target, source);

strcat char source[10] = “There”; char target[10] = “Hi”; strcat(target, source); There\0???? [0][1][2][3][4][5][6][7][8][9] source:

strcat char source[10] = “There”; char target[10] = “Hi”; strcat(target, source); There\0???? [0][1][2][3][4][5][6][7][8][9] source: Hi\0??????? [0][1][2][3][4][5][6][7][8][9] target:

strcat char source[10] = “There”; char target[10] = “Hi”; strcat(target, source); There\0???? [0][1][2][3][4][5][6][7][8][9] source: HiThere\0?? [0][1][2][3][4][5][6][7][8][9] target:

strcat... strcat(target, “!!”); strcat(target, “!”); HiThere\0?? [0][1][2][3][4][5][6][7][8][9] target:

strcat... strcat(target, “!!”); strcat(target, “!”); HiThere!!\0 [0][1][2][3][4][5][6][7][8][9] target:

strcat... strcat(target, “!!”); strcat(target, “!”); HiThere!!! [0][1][2][3][4][5][6][7][8][9] target:

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); cout<<strcmp(ntca1, ntca3); cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”); if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); cout<<strcmp(ntca1, ntca3); cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”); if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”); if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”); if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”); if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); // -1 if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); // -1 if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); // -1 if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”; cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); // -1 if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl; Take Note!

Dangers char source[10] = “Walk This Way”; char target[15]; strcpy(target, source); strcat(target, source);

Also Available  strncpy  strncmp  strncat

End of Session