Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters.

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

 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.
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.
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.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
Programming Character I/O. COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable.
Chapter 9 Character Strings
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
Introduction to C Programming CE Lecture 13 Strings in C.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
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.
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.
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.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
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 8 CHARACTER AND STRINGS
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.
February 14, 2005 Characters, Strings and the String Class.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
C Program Design C Characters and Strings 主講人:虞台文.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
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:
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CSC 270 – Survey of Programming Languages
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
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.
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 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.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
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.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
C ARRAYS.
Chapter 8 “Character Arrays and Strings” Prepared by: Prof. Ajay M. Patel CE, IDS, NU.
INC 161 , CPE 100 Computer Programming
DEPARTMENT OF COMPUTER SCIENCE & APPLICATION
Lecture 8 String 1. Concept of strings String and pointers
CS111 Computer Programming
Chapter 8 Character Arrays and Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings in C Array of characters is called a string.
Std Library of C++.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Presentation transcript:

Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters char s[10] char p[30] When declaring a string don’t forget to leave a space for the null character which is also known as the string terminator character

C offers four main operations on strings strcpy - copy one string into another strcat - append one string onto the right side of the other strcmp – compare alphabetic order of two strings strlen – return the length of a string

strcpy strcpy(destinationstring, sourcestring) Copies sourcestring into destinationstring For example strcpy(str, “hello world”); assigns “hello world” to the string str

Example with strcpy #include main() { char x[] = “Example with strcpy”; char y[25]; printf(“The string in array x is %s \n “, x); strcpy(y,x); printf(“The string in array y is %s \n “, y); }

strcat strcat(destinationstring, sourcestring) appends sourcestring to right hand side of destinationstring For example if str had value “a big ” strcat(str, “hello world”); appends “hello world” to the string “a big ” to get “ a big hello world”

Example with strcat #include main() { char x[] = “Example with strcat”; char y[]= “which stands for string concatenation”; printf(“The string in array x is %s \n “, x); strcat(x,y); printf(“The string in array x is %s \n “, x); }

strcmp strcmp(stringa, stringb) Compares stringa and stringb alphabetically Returns a negative value if stringa precedes stringb alphabetically Returns a positive value if stringb precedes stringa alphabetically Returns 0 if they are equal Note lowercase characters are greater than Uppercase

Example with strcmp #include main() { char x[] = “cat”; char y[]= “cat”; char z[]= “dog”; if (strcmp(x,y) == 0) printf(“The string in array x %s is equal to that in %s \n “, x,y);

continued if (strcmp(x,z) != 0) {printf(“The string in array x %s is not equal to that in z %s \n “, x,z); if (strcmp(x,z) < 0) printf(“The string in array x %s precedes that in z %s \n “, x,z); else printf(“The string in array z %s precedes that in x %s \n “, z,x); } else printf( “they are equal”); }

strlen strlen(str) returns length of string excluding null character strlen(“tttt”) = 4 not 5 since \0 not counted

Example with strlen #include main() { int i, count; char x[] = “tommy tucket took a tiny ticket ”; count = 0; for (i = 0; i < strlen(x);i++) { if (x[i] == ‘t’) count++; } printf(“The number of t’s in %s is %d \n “, x,count); }

Vowels Example with strlen #include main() { int i, count; char x[] = “tommy tucket took a tiny ticket ”; count = 0; for (i = 0; i < strlen(x);i++) { if ((x[i] == ‘a’)||(x[i]==‘e’)||(x[i]==‘I’)||(x[i]==‘o’)||(x[i]==‘u’)) count++; } printf(“The number of vowels’s in %s is %d \n “, x,count); }

No of Words Example with strlen #include main() { int i, count; char x[] = “tommy tucket took a tiny ticket ”; count = 0; for (i = 0; i < strlen(x);i++) { if ((x[i] == ‘ ‘) count++; } printf(“The number of words’s in %s is %d \n “, x,count+1); }

No of Words Example with more than one space between words #include main() { int i,j, count; char x[] = “tommy tucket took a tiny ticket ”; count = 0; for (i = 0; i < strlen(x);i++) { if ((x[i] == ‘ ‘) { count++; for(j=i;x[j] != ‘ ‘;j++); i = j; } printf(“The number of words’s in %s is %d \n “, x,count+1); }

Input output functions of characters and strings getchar() reads a character from the screen in a non-interactive environment getche() like getchar() except interactive putchar(int ch) outputs a character to screen gets(str) gets a string from the keyboard puts(str) outputs string to screen

Characters are at the heart of strings

Exercise 1 Output …………

Exercise 1 #include main() { int i,j; for(j = 1; j <= 10; j++) { for(i=1;i <= j;i++) { printf(“%d “,i); } printf(“\n“); }

Exercise 2 Output * * * * * * ……………. * * * * *

Exercise 2 #include main() { int i,j; for(j = 1; j <= 10; j++) { for(i=1;i <= j;i++) { printf(“* “); } printf(“\n“); }

Exercise 3 Output *********** * ***********

#include main() { int i,j; for(j = 1; j <= 10; j++) { printf(“* “); for(i=1;i <= 8;i++) { if ((j==1) || (j==10)) printf(“* “); else printf(“ “); } printf(“* \n “); }

Some Useful C Character Functions Don't forget to #include to get the function prototypes.

Functions FunctionReturn true if int isalpha(c); c is a letter. int isupper(c); c is an upper case letter. int islower(c); c is a lower case letter. int isdigit(c); c is a digit [0-9].

More Functions FunctionReturn true if int isxdigit(c); c is a hexadecimal digit [0-9A-Fa-f]. int isalnum(c); c is an alphanumeric character (c is a letter or a digit); int isspace(c); c is a SPACE, TAB, RETURN, NEWLINE, FORMFEED, or vertical tab character.

Even More C Functions FunctionReturn true if int ispunct(c); c is a punctuation character (neither control nor alphanumeric). int isprint(c); c is a printing character. int iscntrl(c); c is a delete character or ordinary control character.

Still More C Functions FunctionReturn true if int isascii(c); c is an ASCII character, codeless than int toupper(int c); convert character c to upper case (leave it alone if not lower) int tolower(int c); convert character c to lower case (leave it alone if not upper)

Program to Reverse Strings #include #include int main () { int i; char a[10]; char temp; //clrscr(); // only works on windows gets(a); for (i = 0; a[i] != '\0' ; i++); i--; for (int j = 0; j <= i/2 ; j++) { temp = a[j]; a[j] = a[i - j]; a[i - j] = temp; } printf("%s",a); return(0); }

Program to count the number of vowels in a string : Note Two different ways to declare strings One using pointers *str Two using character array char a[] #include #include void main() { char *str; char a[]="aeiouAEIOU"; int i,j,count=0; clrscr(); printf("\nEnter the string\n"); gets(str); for(i=0;str[i]!='\0';i++) { for(j=0;a[j]!='\0';j++) if(a[j] == str[i] { count++; break; } printf("\nNo. of vowels = %d",count); }