Introduction to C programming

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

 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.
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.
Lecture 24: Strings. 2 Lecture Contents: t Library functions t Assignment and substrings t Concatenation t Comparison t Demo programs t Exercises.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
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.
C-strings Array with base type char One character per indexed variable
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
String Library Calls (Representative, not Exhaustive) Rudra Dutta CSC Spring 2007, Section 001.
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)
Chapter 8 Characters and Strings Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
 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];
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)
Strings, Pointers and Tools
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
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.
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.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Fundamentals of Characters and Strings
CSE 303 Lecture 14 Strings in C
Strings A string is a sequence of characters treated as a group
Arrays in C.
String in C++.
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.
EECE.2160 ECE Application Programming
CPS120: Introduction to Computer Science
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
Characters and Strings Functions
EECE.2160 ECE Application Programming
Presentation transcript:

Introduction to C programming String

What are strings in C? Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0 (or \0 ). For example: char name[50] = “DAVE”;

What are strings in C? (Cont) char a[50] = “This is \n a string.”; printf("This is a string value. Beep! Beep! \7\7"); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... T h i s \n a t r I n g . \0

Conventions What output of printf(a); if a is: What is the length of string a? What if char a[50] does not contain ‘\0’ at all? Are following correct, given char a[6]; a[6]=‘7’; a[5]=“7”; a[5]=7; a++; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... T h i s \0 a t r I n g .

Declarations and Initializing strings Array of char: char str[5] = {'l', 'i', 'n', 'u', 'x'}; char str[6] = {'l', 'i', 'n', 'u', 'x', '\0'}; Declarations

Declarations and Initializing strings #include <stdio.h> #include <string.h> int main() { /* Example 1 */ char string1[ ] = "A string declared as an array.\n"; /* Example 2 */ char *string2 = "A string declared as a pointer.\n"; /* Example 3 */ char string3[30]; strcpy(string3, "A string constant copied in.\n"); printf (string1); printf (string2); printf (string3); return 0; }

Declarations and Initializing strings char string1[] = "A string declared as an array.\n"; This is usually the best way to declare and initialize a string. The character array is declared explicitly. There is no size declaration for the array; just enough memory is allocated for the string, because the compiler knows how long the string constant is. The compiler stores the string constant in the character array and adds a null character (\0) to the end.

Declarations and Initializing strings char *string2 = "A string declared as a pointer.\n"; The second of these initializations is a pointer to an array of characters. Just as in the last example, the compiler calculates the size of the array from the string constant and adds a null character. The compiler then assigns a pointer to the first character of the character array to the variable string2. Note: Most string functions will accept strings declared in either of these two ways.

Declarations and Initializing strings char string3[30]; Declaring a string in this way is useful when you don't know what the string variable will contain, but have a general idea of the length of its contents (in this case, the string can be a maximum of 30 characters long). The drawback is that you will either have to use some kind of string function to assign the variable a value, as the next line of code does ( strcpy(string3, "A string constant copied in.\n");), or you will have to assign the elements of the array the hard way, character by character.

String Array A string array is an array of strings, which, of course, are themselves arrays of characters; in effect, a string array is a two-dimensional character array.

char *menu[] = { " -------------------------------------- ", " | ++ MENU ++ |", " | ~~~~~~~~~~~~ |", " | (0) Edit Preferences |", " | (1) Print Charge Sheet |", " | (2) Print Log Sheet |", " | (3) Calculate Bill |", " | (q) Quit |", " | |", " | Please enter ur choice. |", " -------------------------------------- “ }; int main() { int line; for (line = 0; line < 13; line++) printf ("%s\n", menu[line]); } return 0;

Troubles Because C has no built-in facilities for manipulating entire arrays (copying them, comparing them, etc.), it also has very few built-in facilities for manipulating strings: char firstname[50],lastname[50],fullname[100];   firstname= "Arnold"; /* Illegal */ lastname= "Schwarznegger"; /* Illegal */ fullname= "Mr"+firstname +lastname; /* Illegal */

String library functions The GNU C Library provides a number of very useful functions which handle strings. Here is a list of the more common ones. To use the functions beginning with ato, you must include the header file stdlib.h; to use the functions beginning with str, you must include the header file string.h.

String library functions(1) int atoi(const char *nptr); double atof(const char *nptr); long atol(const char *nptr);

String library functions(1) /*atof Converts an ASCII string to its floating-point equivalent; for example, converts -23.5 to the value -23.5. */ #include <stdio.h> #include <stdlib.h> int main() { double my_value; char my_string[] = "+1776.23"; my_value = atof(my_string); printf("%f\n", my_value); return 0; } The output from the above code is 1776.230000.

String library functions(1) /* atoi Converts an ASCII string to its integer equivalent; for example, converts -23.5 to the value -23. */ int my_value; char my_string[] = "-23.5"; my_value = atoi(my_string); printf("%d\n", my_value); /*atol Converts an ASCII string to its long integer equivalent; for example, converts +2000000000 to the value 2000000000. */ long my_value; char my_string[] = "+2000000000"; my_value = atol(my_string); printf("%ld\n", my_value);

String library functions(2) strcpy strcpy copies a string, including the null character terminator from the source string to the destination. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is: char *strcpy(char *dst, const char *src); strncpy strncpy is similar to strcpy, but it allows the number of characters to be copied to be specified. If the source is shorter than the destination, than the destination is padded with null characters up to the length specified. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is: char *strncpy(char *dst, const char *src, size_t len);

String library functions(2) strcat This function appends a source string to the end of a destination string. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is: char *strcat(char *dst, const char *src); strncat This function appends at most N characters from the source string to the end of the destination string. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is: char *strncat(char *dst, const char *src, size_t N);

String library functions(2) strcmp This function compares two strings. If the first string is greater than the second, it returns a number greater than zero. If the second string is greater, it returns a number less than zero. If the strings are equal, it returns 0. Its prototype is: int strcmp(const char *first, const char *second); strncmp This function compares the first N characters of each string. If the first string is greater than the second, it returns a number greater than zero. If the second string is greater, it returns a number less than zero. If the strings are equal, it returns 0. Its prototype is: int strncmp(const char *first, const char *second, size_t N);

String library functions(2) The strcmp() function lexically compares the two input strings and returns: Less than zero -- if string1 is lexically less than string2 Zero -- if string1 and string2 are lexically equal Greater than zero -- if string1 is lexically greater than string2

String library functions(2) strlen This function returns the length of a string, not counting the null character at the end. That is, it returns the character count of the string, without the terminator. Its prototype is: size_t strlen(const char *str);

String library functions(2) int strcasecmp(const char *s1, const char *s2) -- case insensitive version of strcmp(). int strncasecmp(const char *s1, const char *s2, int n) -- case insensitive version of strncmp().

String library functions(2) To use any of these functions in your code, the header file "strings.h" must be included. It is the programmer's responsibility to check that the destination array is large enough to hold either what is copied or appended to it. C performs no error checking in this respect. See the array tutorial for more details. The data type size_t is equivalent to unsigned integer.

String library functions(2) The use of most of the functions is straightforward, for example: char *str1 = "HELLO"; char *str2; int length; length = strlen("HELLO"); /* length = 5 */ (void) strcpy(str2,str1);

String library functions(2) The strncat(), strncmp,() and strncpy() copy functions are string restricted version of their more general counterparts. They perform a similar task but only up to the first n characters. Note the the NULL terminated requirement may get violated when using these functions, for example: char *str1 = "HELLO"; char *str2; int length = 2; (void) strcpy(str2,str1, length); /* str2 = "HE" */ str2 is NOT NULL TERMINATED!! -- BEWARE

String library functions(3) char *strchr(const char *string, int c) -- Find first occurrence of character c in string. char *strrchr(const char *string, int c) -- Find last occurrence of character c in string. char *strstr(const char *s1, const char *s2) -- locates the first occurrence of the string s2 in string s1. char *strpbrk(const char *s1, const char *s2) -- returns a pointer to the first occurrence in string s1 of any character from string s2, or a null pointer if no character from s2 exists in s1

String library functions(3) size_t strspn(const char *s1, const char *s2) -- returns the number of characters at the begining of s1 that match s2. size_t strcspn(const char *s1, const char *s2) -- returns the number of characters at the begining of s1 that do not match s2. char *strtok(char *s1, const char *s2) -- break the string pointed to by s1 into a sequence of tokens, each of which is delimited by one or more characters from the string pointed to by s2. char *strtok_r(char *s1, const char *s2, char **lasts) -- has the same functionality as strtok() except that a pointer to a string placeholder lasts must be supplied by the caller.

Memory Operations: <memory.h> void *memchr (void *s, int c, size_t n) -- Search for a character in a buffer . int memcmp (void *s1, void *s2, size_t n) -- Compare two buffers. void *memcpy (void *dest, void *src, size_t n) -- Copy one buffer into another . void *memmove (void *dest, void *src, size_t n) -- Move a number of bytes from one buffer lo another. void *memset (void *s, int c, size_t n) -- Set all bytes of a buffer to a given character.

String Input & Output Output: Input: File operations printf(“This is a string: %s”, str1); Input: scanf(“%s%ld%lf%s”, str1, &value1, &value2, str2); gets scanline File operations fopen, fclose, fgets…