Strings #include <stdio.h>

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

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.
Strings.
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.
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
CS Nov 2006 C-strings.
 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.
Introduction to C programming
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
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.
C Program Design C Characters and Strings 主講人:虞台文.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
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.
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
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.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
LINKED LISTS.
C Characters and Strings
INC 161 , CPE 100 Computer Programming
C Characters and Strings
Fundamentals of Characters and Strings
DEPARTMENT OF COMPUTER SCIENCE & APPLICATION
Lecture 8 String 1. Concept of strings String and pointers
Lecture-5 Arrays.
Programming Languages -1 (Introduction to C) strings
CSE 303 Lecture 14 Strings in C
Module 2 Arrays and strings – example programs.
Exercises on String Operations
Strings A string is a sequence of characters treated as a group
Arrays in C.
Programming Languages and Paradigms
Character Strings Lesson Outline
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
CSCI206 - Computer Organization & Programming
C Stuff CS 2308.
CSI 121 Structured Programming Language Lecture 21 Character Strings
CSE1320 Strings Dr. Sajib Datta
INC 161 , CPE 100 Computer Programming
CSE1320 Strings Dr. Sajib Datta
Strings A collection of characters taken as a set:
Functions, Part 2 of 3 Topics Functions That Return a Value
Lecture 11 Strings.
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
Example: ? str1 str2 _ void salin(char sasaran[], char sumber[]);
Conversion Check your class notes and given examples at class.
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
String manipulation string.h library
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Characters and Strings Functions
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to Problem Solving and Programming
Presentation transcript:

Strings #include <stdio.h> #include <stdlib.h> /* needed by atoi( ) and atof( ) */ #include <string.h> /* needed by str...( ) functions */

Copy One Array to Another /* assign a to b */ strcpy( b, a ); printf( "%s\n", b );

Add One Array to Another strcpy( b, a ); strcat ( b, " " ); strcat ( b, a );

Compare Strings /* "Gary Burt" comes before "Mike Burt", so print a negative number */ printf( "%d\n", strcmp( a, c ) ); /* "Mike Burt" comes before "Gary Burt", so print a positive number */ printf( "%d\n", strcmp( c, a ) ); /* "Gary Burt" is the same as "Gary Burt", so print zero */ printf( "%d\n", strcmp( a, "Gary Burt" ) );

Getting Words From a String /* get the first token (delimited by a blank) */ printf( "%s\n", strtok( b, " " ) );

Convert String to Numeric /* convert a string to an integer */ printf( "%d\n", atoi( "1234" ) ); /* convert a string to a float */ printf( "%f\n", atof( "1234.5678" ) );

Find Length Of A String printf( "b is %d characters long\n", strlen( b ) );

Sample Program #include <stdio.h> #include <stdlib.h> /* needed by atoi( ) and atof( ) */ #include <string.h> /* needed by str...( ) functions */ int main( void ) { char a[ 100 ] = "Gary Burt"; char b[ 100 ]; char c[ 100 ] = "Mike Burt"; /* Make sure the array a is as expected */ printf( "%s\n", a );

Sample Program /* assign a to b */ strcpy( b, a ); printf( "%s\n", b ); /* put in a space and add the array a to what is in array a */ printf( "b is %d characters long\n", strlen( b ) ); strcat ( b, " " ); strcat ( b, a );

Sample Program /* "Gary Burt" comes before "Mike Burt", so print a negative number */ printf( "%d\n", strcmp( a, c ) ); /* "Mike Burt" comes before "Gary Burt", so print a positive number */ printf( "%d\n", strcmp( c, a ) ); /* "Gary Burt" is the same as "Gary Burt", so print zero */ printf( "%d\n", strcmp( a, "Gary Burt" ) ); /* get the first token (delimited by a blank) */ printf( "%s\n", strtok( b, " " ) );

/* convert a string to an integer */ printf( "%d\n", atoi( "1234" ) ); Sample Program /* convert a string to an integer */ printf( "%d\n", atoi( "1234" ) ); /* convert a string to a float */ printf( "%f\n", atof( "1234.5678" ) ); return 0; }

Output Gary Burt b is 9 characters long b is 10 characters long Gary Burt Gary Burt -1 1 Gary 1234 1234.567800