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.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to C Programming
C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));
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.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 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.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
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'; }
Sort the given string, without using string handling functions.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
C-strings Array with base type char One character per indexed variable
Data Structure and C Part-6. Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered.
Introduction to C programming
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
February 14, 2005 Characters, Strings and the String Class.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
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.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
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:
COIT29222-Structured Programming Lecture Week 08  Reading: Textbook (4 th Ed.), Chapter 4 Textbook (6 th Ed.), Chapter 7 Study Guide Book 2, Module 11.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
C LANGUAGE Characteristics of C · Small size
 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:
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
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
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
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.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
INC 161 , CPE 100 Computer Programming
Strings A collection of characters taken as a set:
Strings #include <stdio.h>
Characters and Strings Functions
Presentation transcript:

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 is “Daffy”, last is “Duck” fullName is “Daffy Duck”

Solution Example #1 #include int main(void) { char firstName[15] = "Daffy"; char lastName[15] = "Duck"; char fullName[20]; strcpy(fullName,firstName); strcat(fullName, lastName); printf("The full name is %s\n",fullName); return(0); }

Example 2 What is the output from the following code? char player[15]; strcat(player,"Wayne"); strcat(player,"Gretzky"); printf("Player#1: %s\n",player); strcat(player,"Mark"); strcat(player,"Messier"); printf("Player#2: %s\n",player);

Solution Example 2 May have unexpected results since player is not initialized Ifchar player[15]=""; Player1: WayneGretzky Player2: WayneGretzyMarkMessier (OR may crash the system since has more than 15 characters)

Example 3 Consider the 2-D array of names Write the code to find 1.the first name in alphabetical order 2.the last name in alphabetical order Hint: Use the strcmp function char greatOilers[7][15] = { "Gretzky", "Messier", "Smith", "Coffey", "Smyth", "Kurri", "Fuhr" };

Solution Example 3 #include int main(void) { char greatOilers[7][15] = { "Gretzky","Messier", "Smith", "Coffey", "Smyth", "Kurri", "Fuhr" }; char first[15]; char last[15]; int cnt =0, numPlayers=7; strcpy(start,greatOilers[0];) strcpy(last,greatOilers[0];) for (cnt=1; cnt<numPlayers; cnt++) { if (strcmp(start,greatOilers[cnt] < 0) strcpy(first, greatOilers[cnt]); if (strcmp(last,greatOilers[cnt] > 0) strcpy(last, greatOilers[cnt]); } printf("In Alphabetical Order: First: %s Last: %s\n", first, last); return(0); }

Summary 'C' strings are allocated to have a fixed maximum length Be careful when using strcat Use strcpy when appropriate Initialize empty strings to \0 ("") Many more useful string function strlen, strncpy, strncat, strcasecmp, strcoll See glib manual on website

Solution 2 #include int main(void) { char mystr1[] = "What happens to this memory"; char player[16]; char mystr2[] = "Is this memory corrupted"; //strcat(player,"Wayne"); strcpy(player,"Wayne"); strcat(player,"Gretzky"); printf("Player#1: %s\n",player); printf("mystr1 = %s\n\n",mystr1); printf("mystr2 = %s\n\n",mystr2); strcat(player,"Mark"); strcat(player,"Messier"); printf("Player#2: %s\n",player); printf("mystr1 = %s\n\n",mystr1); printf("mystr2 = %s\n\n",mystr2); printf("Addresses: %i %i %i\n", (int) mystr1, (int) player, (int) mystr2); getch(); return(0); }

Output and Explanation Overwrites memory allocated before the string String and addresses show below