 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.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Problem Solving & Program Design in C
Introduction to C Programming
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
Pseudocode A way to make programming easier Start with a verbal description of what the program is supposed to do! Slowly transform it into C, defining.
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 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.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
1 CSE1303 Part A Data Structures and Algorithms Semester 2, 2006 Lecture A1 – Welcome & Revision.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
CS Nov 2006 C-strings.
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.
 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.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
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.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
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.
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.
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.
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:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
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];
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.
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
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.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Quiz 11/15/16 – C functions, arrays and strings
C Stuff CS 2308.
CPS120: Introduction to Computer Science
Strings #include <stdio.h>
Characters and Strings Functions
C Characters and Strings
Introduction to Problem Solving and Programming
Presentation transcript:

 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 is not included in the string.

 There are 2 ways of using strings. The first is with a character array and the second is with a string pointer.

 A character array is declared in the same way as a normal array.  char ca[10];  You must set the value of each individual element of the array to the character you want and you must make the last character a 0. Remember to use %s when printing the string.  char ca[10]; ca[0] = 'H'; ca[1] = 'e'; ca[2] = 'l'; ca[3] = 'l'; ca[4] = 'o'; ca[5] = 0; printf("%s",ca);

char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’}; /*String.c string variable*/ #include main() { char month[15]; printf (“Enter the string”); fgets (month,8,stdin); printf (“The string entered is %s”, month); }

 String pointers are declared as a pointer to a char.  char *sp;  When you assign a value to the string pointer it will automatically put the 0 in for you unlike character arrays.  char *sp; sp = "Hello"; printf("%s",sp);

 You can read a string into only a character array using scanf and not a string pointer.  If you want to read into a string pointer then you must make it point to a character array.  char ca[10],*sp; scanf("%s",ca); sp = ca; scanf("%s",sp);

 The strings.h header file has some useful functions for working with strings. Here are some of the functions you will use most often:

 strcpy(destination,source) You can't just use string1 = string2 in C. You have to use the strcpy function to copy one string to another. strcpy copies the source string to the destination string.  s1 = "abc"; s2 = "xyz"; strcpy(s1,s2); // s1 = "xyz"

 strcat(destination,source) Joins the destination and source strings and puts the joined string into the destination string.  s1 = "abc"; s2 = "xyz"; strcat(s1,s2); // s1 = "abcxyz"

 strcmp(first,second) Compares the first and second strings. If the first string is greater than the second one then a number higher than 0 is returned. If the first string is less than the second then a number lower than 0 is returned. If the strings are equal then 0 is returned.  s1 = "abc"; s2 = "abc"; i = strcmp(s1,s2); // i = 0

 strlen(string) Returns the amount of characters in a string.  s = "abcde"; i = strlen(s); // i = 5

#include #include void main() { char s1[20],s2[20],s3[20]; int x,l1,l2,l3; printf(“Enter the strings”); scanf(“%s%s”,s1,s2); x=strcmp(s1,s2); if(x!=0) {printf(“\nStrings are not equal\n”); strcat(s1,s2); } else printf(“\nStrings are equal”); strcpy(s3,s1); l1=strlen(s1); l2=strlen(s2); l3=strlen(s3); printf(“\ns1=%s\t length=%d characters\n”,s1,l1); printf(“\ns2=%s\t length=%d characters\n”,s2,l2); printf(“\ns3=%s\t length=%d characters\n”,s3,l3); }

 Converting strings to numbers  char *str1=“123.79”;  char *str2 = “3”;  float x;  int y; x = atof(str1); y = atoi(str2);

 ctype.h has character handling functions  since a string is a character array, can convert to uppercase as follows: char name1[]=“Scooby”; int x; for (x=0; x <=strlen(name1); x++) name1[x] = toupper(name1[x]);

 ctype.h has character handling functions  since a string is a character array, can convert to lowercase as follows: char name1[]=“Scooby”; int x; for (x=0; x <=strlen(name1); x++) name1[x] = tolower(name1[x]);

1) Write a program that performs the following:  uses character arrays to read a user’s name from standard input (stdin)  tells the user how many characters are in their name  displays the user’s name in uppercase  displays the user’s name in lowercase  changes the user’s name to append “man” on the end – e.g. Jack becomes Jackman – and prints it out 2) Look up the strstr() function (page 196) and write a program that uses it to search for the last occurrence of a substring in a string. Print out the number of occurrences of the substring in the string.