Introduction to Computer Algorithmics and Programming Ceng 113 Serap ATAY Pointers _ Part 2.

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
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 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.
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 Introduction to Computing: Lecture 16 Character Strings Dr. Bekir KARLIK Yasar University Department of Computer Engineering
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Pointers Ethan Cerami Fundamentals of Computer New York University.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to C Programming CE Lecture 13 Strings in C.
Introduction to C Programming CE
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
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.
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.
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 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
Introduction to Computer Algorithmics and Programming Ceng 113 Arrays and Strings.
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:
Computer Programming Control Structure
© 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:
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
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
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
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.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
char first[10]="monkey"; char second[10]="fish"; char* keep;
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
CSE 220 – C Programming Pointers.
ECE Application Programming
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Command Line Arguments
Functions, locals, parameters, and separate compilation
Pointers Department of Computer Science-BGU יום שלישי 31 יולי 2018.
Computer Science 210 Computer Organization
POINTERS.
Computer Science 210 Computer Organization
CS111 Computer Programming
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
CSE1320 Strings Dr. Sajib Datta
EECE.2160 ECE Application Programming
Chapter 16 Pointers and Arrays
Simulating Reference Parameters in C
Character Arrays char string1[] = “first”;
Strings #include <stdio.h>
Programming Strings.
Characters and Strings Functions
Presentation transcript:

Introduction to Computer Algorithmics and Programming Ceng 113 Serap ATAY Pointers _ Part 2

What is wrong? 1.*s + 1 = ‘x’; 2.*(s+1) = ‘x’; 3.int p;.... *p=‘a’; 4.c = a * *b; 5.char ch; char *p;... ch = ‘a’; p = &ch;

What is wrong? #define MAX char *p;... p = &MAX; ERROR; The address operator can be used with only objects. MAX is only a logical constant.

Pointer increment/decrement char *p; int a;... p=p + a; p=p – 2; p += a; p= p + a + 10; ++p;

Sample “Swap operation” #include void swap(int *x, int *y); void main() {int a=10, b= 20; swap(&a, &b); printf(“a= %d b = %d \n”, a, b); } void swap(int *x, int *y) {int temp; temp = *x; *x=*y; *y= temp; }

Sample “Array operation” #include int max(int *p, int n); void main() {int a[10]={10,-4,5,9,71,98,89,4,8, 10}; int m; m= max(a, 10); printf(“The greatest number=%d \n”, m); } int max(int *p, int n) {int maxval, k; maxval = p[0]; for (k=1; k <n; ++k) if (maxval < p[k]) maxval = p[k]; return maxval; } int max(int *p, int n) {int maxval, k; maxval = 0; for (k=0; k <n; ++k) { if (maxval < *p) maxval = *p; p++; } return maxval; }

Sample “Array operation”

Sample #include void getname(char *s); void main() {char str[50]; getname(str); printf("%s \n", str); } void getname(char *s) {printf("Name - Last name:"); gets(s); }

Sample “Split String” Suppose we are given a string of the form "Last_name/First_name." We want to split this into two strings, one containing the first name and one containing the last name. We need a function to find the slash in the name. my_strchr  Finds a character in a string. Parameters: string_ptr  String to look through. find  Character to find. Returns: pointer to 1st occurrence of character in string or NULL for error.

Sample “Split String”

#include int main() { char line[80]; /* The input line */ char *first_ptr; /* pointer to the first name */ char *last_ptr; /* pointer to the last name */ gets(line); last_ptr = line;/* last name is at beginning of line */ first_ptr = my_strchr(line, '/'); /* Find slash */ /* Check for an error */ if (first_ptr == NULL) { printf("Error: Unable to find slash in %s\n", line); exit (0); } *first_ptr = '\0'; /* Zero out the slash */ ++first_ptr; /* Move to first character of name */ printf("First:%s Last:%s\n", first_ptr, last_ptr); return (0); }

Sample “Split String” char *my_strchr(char *string_ptr, char find) { while (*string_ptr != find) { /* Check for end */ if (*string_ptr == '\0') return (NULL); /* not found */ ++string_ptr; } return (string_ptr); /* Found */ }

Sample _ String Copy // This program is copied one string to another and use pointers. # include void string_copy(char *source, char *dest); void main () {char str_from[80], str_to[80]; gets(str_from); str_to[0]='\0'; string_copy(str_from, str_to); puts(str_to); } void string_copy(char *source, char *dest) { while *source != '\0'); { *dest++ = *source++; } *dest = '\0'; } void string_copy(char *source, char *dest) {while *source != '\0'); { *dest = *source; dest++; source++; } *dest = '\0'; } void string_copy(char *source, char *dest) { while ((*dest++ = *source++) != '\0'); }

Sample – “String Compare” #include int str_compare(char *str1, char *str2); void main() {char s1[20], s2[20]; int result=0; printf(“Input the first string: \n”); gets(s1); printf(“Input the second string: \n”); gets(s2); result = str_compare(s1, s2); if (result == 0) printf(“The strings are same.\n”); else printf(“The strings are different. \n”); } int str_compare(char *str1, char *str2) {while (*str1 == *str2) {if (*str1 == ‘\0’) return 0; ++str1; ++str2; } return *str1 - *str2; }

Homework Write an algorithm and C code to count the vowels and letters in a string. –Read a string from the screen. –Count the vowels and letters Then print out the –number of occurrences of each of the vowels a, e, ı, i, o, ö, ü and u in the text, –the total number of letters, and –each of the vowels as an integer percentage of the letter total. Suggested output format is: Numbers of characters: a 3 ; e 2 ; i 0 ; o 1 ; u 0 ; rest 17 Percentages of total : a 13%; e 8%; i 0%; o 4%; u 0% ; rest 73% Use pointers for all string, comparison and calculation operations. Upload to FTP until December 14th. 2006

Quiz Question Write a program, which is concatenate second string to the end of the first string. For example: string_1= “Happy” string_2=“New year” After operation string_1=“HappyNew year” Use pointers for all array operations. Do not use “strlen” standard function.

String Concatenate #include char *string_cat(char *s1, char *s2); void main() {char str1[50], str2[50]; char *p; gets(str1); gets(str2); p = string_cat(str1, str2); printf("%s \n", p); } char *string_cat(char *s1, char *s2) {char *temp; temp = s1; while (*s1 != '\0') s1++; while ((*s1++ = *s2++)!= '\0'); return temp; }

Next Course Functions