Sharif University of Technology
Department of Computer Engineering
1
Strings 1 Sharif University of Technology Department of Computer - - PowerPoint PPT Presentation
Strings 1 Sharif University of Technology Department of Computer Engineering Input and Output Lecture 4 Introduction Until now We have seen strings in printf Our old definition: string is a set of char between
Sharif University of Technology
Department of Computer Engineering
1
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
2
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
3
'p' 'r' 'o' 'g' 'r' 'a' 'm' '\0'
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
4
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
5
– Example: char s [10];
– By convention, C uses a NULL character '\0' to terminate all strings in its library functions
char str [10] = {'u', 'n', 'I', 'x', '\0'};
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
6
a b c d e z … … … … 97 98 99 100 101 ………………………112 A B C D E Z … … … … 65 66 67 68 69 ……………………… 90 1 2 3 4 9 8 7 6 5 48 49 50 51 52 53 54 55 56 57 \0 \n 10
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
7
char s[10]; s[0] = 'h'; s[1] = 'i’; s[2] = '!'; s[3] = '\0';
– For example:
c = s[1]; if (s[0] == '-') … switch (s[1]) ...
? ? ? ? ? ? \0 ! i h [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] s
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
8 8
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
9
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
10
char str[11]="unix and c"; printf("%s", str); printf("\n"); str[6]='\0'; printf("%s", str); printf("\n"); printf("\n"); printf(str); printf("\n"); str[2]='I'; printf(str); printf("\n");
\0 c d n a x i n u [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] str \0 c d \0 a x i n u [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] str \0 c d \0 a x I n u [10] [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] str
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
11
H e l l
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
12
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
13
H e l l
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
14
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
15
– a neg value if S1 < S2, – 0 if S1 == S2 – a pos value if S1 > S2
H e l l
H e l
l < o in lexicographic order
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
16
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
17
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
18
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
19
char sentence[] = "The quick brown fox"; puts(sentence); // printf("The quick brown fox\n");
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
20
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
21
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
22
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
23
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
24
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
25
char s1[] = "abc"; char s2[] = "abc"; i = strcmp(s1, s2); //i = 0 char s3[] = "abc"; char s4[] = "abx"; i = strcmp(s3, s4); //i = -1 char s5[] = "axc"; char s6[] = "abc"; i = strcmp(s5, s6); //i = 1 char s7[] = "ab"; char s8[] = "abc"; i = strcmp(s7, s8); //i = -1 char s9[] = "abc"; char s10[] = "aBc"; i = strcmp(s9, s10); //i = 1
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
26
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
27
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
28
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
29
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
30
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
31
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
32
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
33
#include <string.h> #include <stdio.h> int main() { char str1[10] = "abc"; char str2[100]; printf("%d\n", strlen(str1)); strcpy(str2, str1); puts(str2); puts("\n"); // printf("\n\n"); strcat(str2, str1); puts(str2); }
Input and Output – Lecture 4
Sharif University of Technology
Department of Computer Engineering
34