strncat s3 s1 6 Happy strcat s3 s1 Happy Happy New Year Quick yak Ask students

Strncat s3 s1 6 happy strcat s3 s1 happy happy new

This preview shows page 25 - 34 out of 34 pages.

strncat( s3, s1, 6 ) = Happy strcat( s3, s1 ) = Happy Happy New Year Quick yak: Ask students to print their names as: Ankur Sharma Sharma Ankur Ankur S. A. Sharma
Image of page 25
©LPU CSE101 C Programming Comparison Functions of the String Handling Library Comparing strings Computer compares numeric ASCII codes of characters in string strcmp() Compares its first string argument with its second string argument, character by character. Function strncmp() does not compare characters following a null character in a string.
Image of page 26
©LPU CSE101 C Programming strcmp() int strcmp( const char *s1, const char *s2 ); Compares string s1 to s2 Returns a negative number if s1 < s2 , zero if s1 == s2 a positive number if s1 > s2 Quick yak: Comparison of strings: Meghalaya Manipur To be discussed
Image of page 27
©LPU CSE101 C Programming strncmp() int strncmp( const char *s1, const char *s2, int n); Compares up to n characters of string s1 to s2 a negative number if s1 < s2 , zero if s1 == s2 a positive number if s1 > s2
Image of page 28
©LPU CSE101 C Programming Example Code This program demonstrates string comparison functions: strcmp() and strncmp()
Image of page 29
©LPU CSE101 C Programming Output s1 = Happy New Year s2 = Happy New Year s3 = Happy Holidays strcmp(s1, s2) = 0 strcmp(s1, s3) = 1 strcmp(s3, s1) = -1 strncmp(s1, s3, 6) = 0 strncmp(s1, s3, 7) = 1 strncmp(s3, s1, 7) = -1 Quick yak: Students to create a friendsh ip game: Based on the score out of -1,0,1 of frst names of two people….
Image of page 30
©LPU CSE101 C Programming Determining the length of string strlen() Function strlen in #include<string.h> Function strlen() takes a string as an argument and returns the number of characters in the string the terminating null character is not included in the length
Image of page 31
©LPU CSE101 C Programming #include <stdio.h> #include <string.h> void main() { /* initialize 3 char pointers */ const char *string1 = "abcdefghijklmnopqrstuvwxyz"; const char *string2 = "four"; const char *string3 = "Boston"; printf( "%s\"%s\"%s%d\n%s\"%s\"%s%d\n %s\"%s\"%s%d\n" , "The length of “ ,string1, "is" , strlen(string1) , "The length of “ ,string2, ”is“ , strlen(string2) , "The length of “ ,string3, ”is“ , strlen(string3) ); } /* end main */ Program demonstrates string length function strlen()
Image of page 32
The length of "abcdefghijklmnopqrstuvwxyz" is 26 The length of "four" is 4 The length of "Boston" is 6
Image of page 33
©LPU CSE101 C Programming [email protected] Next Lecture Mining different types of data from string …?? String conversion functions
Image of page 34

You've reached the end of your free preview.

Want to read all 34 pages?

  • Spring '20
  • ASCII, char array

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture