Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.

Slides:



Advertisements
Similar presentations
 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.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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.
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.
 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.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
Topic 10 - Strings.
Introduction to C Programming CE Lecture 13 Strings in C.
Character Arrays strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */ char pmessage[] = "now.
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 Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Computer Science 210 Computer Organization Strings in C.
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.
The C Programming Language
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.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
CSC 270 – Survey of Programming Languages C Lecture 4 – Strings Heavily borrowed from Dr. Robert Siegfried and Dietel How to Program in C Powerpoint Presentations.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
C Programming Lecture 5 : Basic standard I/O Lecture notes : courtesy of Ohio Supercomputing Center, science and technolgy support.
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)
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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’};
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.
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:
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 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];
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
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
ENEE150 – 0102 ANDREW GOFFIN Strings. Project 2 Flight Database 4 options:  Print flight  Print airport  Find non-stop flights  Find one-stop flights.
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.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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.
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.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
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
Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Command Line Arguments
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Strings.
Strings.
Strings What is a string? It is an array of characters terminated with
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings #include <stdio.h>
Characters and Strings Functions
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
Introduction to Problem Solving and Programming
Presentation transcript:

strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; *s != ‘\0’ ; s++) n++; return n; }

strcpy() implementation /* strcpy : copy t to s */ void strcpy(char *s, char *t) { int i=0; while ((s[i] = t[i]) != ‘\0’) i++; } /* strcpy : copy t to s */ void strcpy(char *s, char *t) { while ((*s = *t) != ‘\0’) { s++; t++; } /* strcpy : copy t to s */ void strcpy(char *s, char *t) { while (*s++ = *t++) ; }

strcmp() implementation /* strcmp : return 0 if s>t int strcmp(char *s, char *t) { int i; for (i = 0; s[i] == t[i]; i++) if (s[i] == ‘\0’) return 0; return s[i] – t[i]; } int strcmp(char *s, char *t) { for ( ; *s == *t; s++, t++) if (*s == ‘\0’) return 0; return *s - *t; }

#include #define MAX_LINE 81 int main() { char s[] = "Hello", t[6]; char *p = "world", *q; printf(“string s = %s\n", s); strcpy(t,s); printf(“string t = %s\n", t); printf(“string p = %s\n", p); q = p; printf(“string q = %s\n", q); strcpy(s, "Good"); printf(“string s = %s\n", s); printf(“string t = %s\n", t); strcpy(p, "Bye"); printf(“string p = %s\n", p); printf(“string q = %s\n", q); return 0; }

String char *p =“Hello”; char m[]=“world”; H e l l o \0 p m m[0]m[1]m[2]m[3]m[4]m[5] w o r l d \0 p[0]p[1]p[2]p[3]p[4]p[5]

String input Example1) char *name; scanf("%s", name); Example2) char name[81]; scanf("%s", name); Example3) char *name; name=(char*)malloc(sizeof(char)*81); scanf("%s", name); … free(name); // deallocate when name is no longer useful

Multiple String Using 2 dimensional array char colors[3][10]= {"red", "blue", "white"}; or char *colorp[3] = {"red", "blue", "white"};

Multiple String r e d \0 colors[0] colors[1] colors[2] b l u e \0 w h i t e \0 r e d \0 b l u e \0 w h i t e \0 colorp[0] colorp[1] colorp[2]

String Input/Output char *gets(char *str); Read one line string from keyboard Put the input string into str int puts(char *str); Print string str into standard output

String Input/Output int sprintf(char *str, char *format,...); Put the output into str instead of standard output int sscanf(char *str, char *format,...); Get the input from str instead of standard input

Output : C programming language C programming language C programming language is beautiful

Other String functions strcpy, strcat, strcmp, strlen int atoi(char *str); // ascii to integer double atof(char *str); // ascii to double char *strstr(char *str1, char *str2); // search for str2 in str1

#include #define MAX_LINE 81 int main() { float sum = 0; int count = 0; char num[MAX_LINE]; printf(“get price : \n"); while (gets(num) != NULL) { count++; sum = sum + atof(num); } printf(“ %d items, Sum : %6.2f \n", count, sum); return 0; } Output : get price : ^Z 4 items, Sum :

Main arguments int main(int argc, char *argv[]) argc : number of arguments argv argv[0] : execution file name argv[1] : first argument string argv[2] : second argument string …

C\:> echo hello world 실행결과 : C:> echo hello world hello world