Dr. Sajib Datta Feb 21, 2014.  In the last class we discussed: ◦ Bubble sort  How it works  performance.

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

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
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.
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.
Chapter 9 Character Strings
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
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 Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
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.
Yu Yuanming CSCI2100B Data Structures Tutorial 3
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CS 108 Computing Fundamentals March 26, Class Notes Last day to withdraw from a class is Monday, April 6 Next week I sent s to a small number.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
The char Data Type A char is a one byte integer type typically used for storing characters. Example: char oneLetter = ’D’; We enclose the character in.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
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.
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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Characters and Strings
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
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.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Introduction to Programming Using C Arrays. 2 Contents Arrays Subscripting.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
INC 161 , CPE 100 Computer Programming
Lecture 8 String 1. Concept of strings String and pointers
CSE 1320 Search, Sort and Strings
CSE1320 Loop Dr. Sajib Datta
Functions Dr. Sajib Datta
A First Book of ANSI C Fourth Edition
Module 2 Arrays and strings – example programs.
Arrays in C.
CSE1320 Files in C Dr. Sajib Datta
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
INC 161 , CPE 100 Computer Programming
CSE1320 Strings Dr. Sajib Datta
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Chapter 8 Character Arrays and Strings
String manipulation string.h library
Exercise Arrays.
Presentation transcript:

Dr. Sajib Datta Feb 21, 2014

 In the last class we discussed: ◦ Bubble sort  How it works  performance

 A string is a series of one or more characters, terminated by a null character.  ◦Example  ◦“I am at UTA”  The double quotation marks are not part of the string.  They just inform the compiler they enclose a string,  just as single quotation marks identify a character.

 C does not have a string type.  Characters in a string are stored in adjacent memory cells, one character per cell, and an array consists of adjacent memory locations, so placing a string in an array is quite natural.  We use an array of chars for storing a string.  We can declare this just like we did with other types.  Example  char letters[12] = “I am at UTA”; IamatUTA\0

 \0 is the null character which is used by C for marking the end of a string  The null character is not the digit zero; it is the nonprinting character.  Its ASCII code value is 0.  The presence of the null character means the array must have at least one more cell than the number of the characters to be stored

 Even though we define a char array much larger than a string, the null character will tell the compiler where is the end of the string. This is useful when we call printf( ).  char letters[15] = “I am at UTA”; IamatUTA\0

 gets() – read a line from input  Reads characters from stdin and stores them as a string into str until a newline character ('\n').  The ending newline character ('\n') is not included in the string.  A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.  Notice that gets() does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid overflows.

 puts(the name of your char array) print out a string.  #include  int main(void)  {  char myString[41];  //printf("Please intput a string with characters <= 40:\n");  puts("Please intput a string with characters <= 40:\n");  gets(myString);  puts(myString);  return 0;  }  Note that all input will be regarded as one string.  However, scanf(“%s”, myString) will only read the characters until the first space as one string.

 Scan a string and print it out  #include  int main(void)  {  char name[40];  printf("What's your name?\n");  scanf("%s", name);  printf("Hello, %s.\n", name);  return 0;  }   %s is the format specifier for a string.  The name of a char array represents an address in computer memory, therefore, we don‟t need “&” when we call scanf function.

 If your input is ◦ Sajib Datta  Just calling scanf() once will only store Sajib in the char array as a string.  The remaining of the input will be ignored.  In this case, the space is used to separate strings in a sentence.

 Scan a string and print it out  #include  int main(void)  {  char name[40];  printf("What's your name?\n");  gets(name);  printf("Hello, %s.\n", name);  return 0;  }   If you input “Sajib Datta”, it will print “Hello Sajib Datta.”

1. char a[2] = {‘x’,’y’}; printf(“%s”, a); // puts(a); 2. char a[3] = {‘x’,’y’}; printf(“%s”,a); //puts(a); 3. char a[2] = “xy”; printf(“%s”,a); //puts(a); 4. char a[3] = “xy”; printf(“%s”,a); //puts(a); 5. char a[5] = {‘’x’,’y’,’z’,’\0’,’d’}; printf(“%s”,a); //puts(a);

6. char a[5] = “xyz\0d”; printf(“%s”,a); //puts(a); 7. char a[5] = “xy\\0”; printf(“%s”,a); //puts(a);

 #include  int main(void)  {  char text[100];  int size, i;  printf("Enter a string: ");  scanf("%s", text);  printf("you entered %s\n", text);  size = strlen( text );  printf("The number of characters in the input string is %d.\n", size);  return 0;  }  The strlen function returns the number of characters in a string, excluding „\0‟.

 Now if we access all characters of a string in a loop, we have two approaches to terminate the loop:  (1) the length of the string ---- strlen()  (2) ‘\0’

 Print out the reversed string  #include  int main(void)  {  char text[100];  int size, i;  printf("Enter a string: ");  scanf("%s", text);  printf("you entered %s\n", text);  size = strlen( text );  for(i = size - 1; i >= 0; i--)  printf("%c", text[i]);  printf("\n");  }

 #include  int main(void)  {  char text[100];  int size, i, upper = 0, lower = 0;  printf("Enter a string: ");  scanf("%s", text);  size = strlen( text );  for(i = 0; i < size; i++)  {  if('A' <= text[i] && text[i] <= 'Z')  upper++;  else if('a' <= text[i] && text[i] <= 'z')  lower++;  } printf("upper = %d, lower = %d\n", upper, lower);  }

 strcmp(str1, str2)  This function starts comparing the first character of each string.  If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.

 A zero value indicates that both strings are equal.  A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2;  And a value less than zero indicates the opposite.

 #include  int main(void)  {  char str1[] = "cat", str2[] = "car";  printf("%d", strcmp(str1, str2));  return 0;  }