Unit 4 - I Strings v1.2.pdf - TM\u2019s C Programming Lecture Notes Ver 1.2(For non-commercial educational use only Lecture notes C Programming Unit 4 Part

Unit 4 - I Strings v1.2.pdf - TMu2019s C Programming...

This preview shows page 1 - 6 out of 14 pages.

TM’s C Programming Lecture Notes Ver. 1.2 (For non-commercial educational use only) 1 Lecture notes: C Programming Unit 4 Part 1 Strings Ver. 1.2
Image of page 1
TM’s C Programming Lecture Notes Ver. 1.2 (For non-commercial educational use only) 2 Contents 1. Strings in C ..................................................................................................................................................................... 3 1.1 Common operations on strings ................................................................................................................................. 3 1.2 Declaring and initializing strings .............................................................................................................................. 3 Initializing: ................................................................................................................................................................. 3 2. Reading string from user ................................................................................................................................................ 4 3. Writing strings to screen ................................................................................................................................................. 5 4. Operations on Strings without using built-in functions .................................................................................................. 6 4.1 Finding the length of a string. .................................................................................................................................. 6 4.2 Copy String Without Using built-in function strcpy() .............................................................................................. 7 4.3 Concatenate Two Strings Without Using built-in function strcat() .......................................................................... 8 4.4 Reversing a string ..................................................................................................................................................... 9 4.5 Comparing two strings ........................................................................................................................................... 10 5. String handling functions in C(SLE) ............................................................................................................................ 11
Image of page 2
TM’s C Programming Lecture Notes Ver. 1.2 (For non-commercial educational use only) 3 1. Strings in C In C, String is sequence of characters treated as a single data item. E.g. “Hello world”, “Well done” Note: double quotes not part of string In C, Strings are represented using character arrays. E.g: char name[20]; 1.1 Common operations on strings Reading & Writing strings (I/O) Combining strings Copying Strings Comparing strings for equality Extracting portion of a string(sub-string) 1.2 Declaring and initializing strings General format: char string_name [size]; E.g. char name[20], city[20]; Initializing: E.g.1: char city[9] = “New York”; E.g. 2: char city[9] = {„N‟,‟e‟,‟w‟,'',‟Y‟,‟o‟,‟r‟,‟k‟,‟ \ 0‟}; Null character „ \ 0‟ inserted by compiler in e.g.1. Specify array size considering „ \ 0‟ also. E.g.: char city[9] = “New York”; char str[9] = “Good”; // Valid initialization char str1[9] ; str1 = “Good”; // Error: Not allowed to assignment like this in C Null character(\0) marks the end-of-string. PRG416:String: compile time initialization // String: compile time initialization #include<stdio.h> int main() { // Two ways to initialize, which are equivalent char str1[10] = "New York"; char str2[10] = {'N','e','w','','Y','o','r','k','\0'};
Image of page 3
TM’s C Programming Lecture Notes Ver. 1.2 (For non-commercial educational use only) 4 printf("str1:\t%s\n",str1); printf("str2:\t%s\n",str2); return 0; } Output: str1: New York str2: New York 2. Reading string from user Using scanf() function. char address[9]; scanf("%s", address); If user enters New York for above code, it is stored as: ? ’s may be unknown random/garbage characters Normally scanf stops reading input when whitespace is encountered . Try reading as two string: scanf("%s %s", addr1,addr2); Using scanf function: Reading specified number of characters char address[9]; scanf("%4s", address); // Only first 4 charcters of input string read. If user enters London , only 4 characters read: PRG417:String input from user(%ws) // String input from user(%ws) #include<stdio.h> void main() { char str1[10]; char str2[10]; printf("Enter the string:"); scanf("%s",str1); scanf("%4s",str2); printf("str1:\t%s\n",str1); printf("str2:\t%s\n",str2); } o/p Enter the string:hello world str1: hello str2: worl // w=4 so only 4 chars read from user
Image of page 4
TM’s C Programming Lecture Notes Ver. 1.2 (For non-commercial educational use only) 5 General format scanf( “%ws”,string_ variable); If entered string longer than w, only w characters read.
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 14 pages?

  • Spring '20
  • ASCII, scanf

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture