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.

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.
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.
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.
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.
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'; }
Pointers in C Rohit Khokher
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
CS 241 Section Week #2 2/4/10. 2 Topics This Section MP1 overview Part1: Pointer manipulation Part2: Basic dictionary structure implementation Review.
Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002.
Strings String - a string is a series of characters treated as a unit. A C string is a variable-length array of characters that is delimited by the null.
CS Nov 2006 C-strings.
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.
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.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
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.
Introduction to C programming
While Loop Structure while (condition) { …. // This line will process when while condition is true }
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
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.
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)
Lecture 10 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Chapter 8: Character and String CMPD144: Programming 1.
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. 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];
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.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
Fundamentals of Characters and Strings
EECE.2160 ECE Application Programming
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
Presentation transcript:

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

C String is a sequence of one or more characters terminated by a NULL ( '\0' )character. The NULL character is crucial in determining the end of the string.

Goal Replay.. Write a function that takes an array as input and returns the length of the string.

int strlen(char str[ ]){ int i=0; while (str[i] != '\0') i++; return i; } int strlen(char * str){ int i=0; while (str[i] != '\0') i++; return i; } int strlen(char * str){ char * p = str; while(*p) p++; return p - str; } int strlen(char * str){ int i=0; while (str[i++]) ; return i - 1; }

size_t strlen(const char *s); –return length of string (# of chars preceding '\0') char *strcpy (char *dest, const char *src); –copies the string src into dest (including NULL-char); returns dest. char *strcat (char *dest, const char *src); –concatenates src to the end of dest, adds NULL at the end of dest; returns dest. int strcmp (const char *s1, const char *s2); –compares strings s1 and s2, returns a negative value if s1 is lexicographically less than s2; zero if s1 is equal to s2; a positive number if s1 is greater than s2.

char * strchr(const char *s, char c); –returns a pointer to the first occurrence of c in the string s. (NULL if not found) char * strrchr(const char *s, char c); –returns a pointer to the last occurrence of c in the string s. (NULL if not found) char * strstr(const char *s1, const char *s2); –returns a pointer to the first occurrence of s2 in string s1.

char *strncpy (char *dest, const char *src, size_t n); –copies at most the first n characters of src into dest. char *strncat (char *dest, const char *src, size_t n); –concatenates at most n characters of src to the end of dest. int strncmp (const char *s1, const char *s2); –compares at most max(n, s1.length, s2.length) characters.

size_t : an unsigned integral type defined in const char* : string parameters that are not modified by the function.

#define MAX 32 char x[MAX], y[MAX]; printf("%d", strlen("hello."); printf("%s", strcpy(x, "green")); printf("%s", strcpy(y, strrchr("blue", 'u') ) ); x[2] = '\0'; printf("%s", strcat(x, y) ); printf("%s", strcpy(x, y) ); printf("%d", strcmp(x,y) ); printf("%s", strchr("blackwhite", 'w') );

Goal write your own strcpy(s1, s2) function. –copies s2 into s1, including NULL-char. –returns s1

char * my_strcpy(char *dest, char *src) { char *to=dest, *from=src; while(*to = *from) to++, from++; return dest; } char * my_strcpy(char *dest, char *src) { char *to=dest, *from=src; while(*to++ = *from++) ; return dest; }

Goal Write your own strcat(s1, s2) function –appends s2 at the end of s1.

char * my_strcat (char *to, char *from) { strcpy(to + strlen(to), from); return to; }

Goal Write strstr(s1,s2) function that locates the first occurrence of s2 in s1.

char * my_strstr(char * str, char * find) { char * p; for(p = str; p = strchr(p, *find); p++) if(!strncmp (p, find, strlen(find)) return p; return NULL; }

String Arrays char days[7][32] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; printf( "%s", days[2] );

void main(){ char str[ ] = "flower", *p; int i; for(p = &str[5]; p >= &str[0]; ) printf("%c", *p--); printf("\n"); for(p = str+5, i=0; p-i >= str; ) printf("%c", *(--p - i++) ); printf("\n"); for(p = &str[5], i=0; i<=5; i++) printf("%c", p[-i]); printf("\n"); for(p = str+5; p >= str; p--) printf("%c", str[p-str] ); printf("\n"); }

Goal: int-sort cheat. Read student grades (integers: 0-100) into an array, and print in ascending order.

Goal: buble-sort. Write a program that reads a list of floats, stores them in an array, sorts the numbers in ascending order, and prints the ordered list.

#include #define MAX_SIZE 1000 void ReadList(float a[ ], int size); void PrintList(float a[ ], int size); void BubbleSort(float a[ ], int size); void main( ) { int arr[MAX_SIZE], size; ReadList(arr, size); PrintList(arr, size); BubbleSort(arr, size); PrintList(arr, size); }

void ReadList(float a[ ], int size) { int i=0; for( ; i<size; i++) scanf("%f", &a[i] ); } void PrintList(float a[ ], int size) { int i=0; for( ; i<size; i++) printf("%f, ", a[i]); printf("\n"); }

void BubbleSort(float a[ ], int size) { int i, unsorted; float f; do { unsorted = 0; for(i=0; i<size-1; i++) if(a[i] > a[i+1]) { f = a[i]; a[i] = a[i+1]; a[i+1] = f; unsorted++; } size--; } while(unsorted); }

Goal: SelectionSort Write a function that sorts an array of floats using selection sort algorithm.