IPC144 Library Functions

IPC144 Library Functions

<p> IPC144 – Library Functions Agenda:</p><p>1 Review/Questions 2 Date functions from time.h 3 Character classification functions from ctype.h 4 More stdlib.h functions – atoi() and atof() 5 More stdio.h and string.h functions 6 Try it! 7 Homework</p><p>Review/Questions</p><p>Date functions from time.h time.h library has a collection of functions to work with time and dates. See example - get_cur_date.c on how you can get a formatted current date. </p><p>Character Classification functions from ctype.h isdigit(), isspace(), isupper() and more character classification functions are defined in the ctype library and are used for testing the character passed. The syntax of these functions are similar, one parameter is accepted – an integer representing ASCII code of a character to examine. It returns 0 if the character is not what is expected, positive value if it is. </p><p>The syntax of these functions is as follows: int isdigit(int);</p><p>See example: fun57.c </p><p>More stdlib.h functions - atoi() and atof() atoi() and atof() functions are used to convert a number from string form into numeric form. The main advantage of converting the numbers from string form into numeric form is the numeric form allows you to perform calculations. </p><p>The syntax of these functions is as follows: int atoi(const char *); double atof(const char *); Example 1: Example 2: char str[] = “1999”; char str[] = “60.99”; int year = 0; double price = 0; year = atoi(str); price = atof(str); printf(“Year: %s \n”, str); printf(“Price: %s\n”, str); printf(“Next year is: %d\n”, year + 1); printf(“Price + tax: %.2lf\n”, price + price * 0.15);</p><p>See example of using atoi() function - review fun58.c </p><p>More stdio.h functions - more scanf()???? </p><p> sscanf() reads formatted input from a string. </p><p>The syntax of this function is as follows: int sscanf(const char *str, const char *format, …. )</p><p>Example 1: #include <stdio.h> int main(){ char s1[31] = "Flight 815"; char w[31]; int num = 0; int ret = 0;</p><p> ret = sscanf(s1, "%s%d", w, &num); printf("ret is %d, w is %s, num is %d\n", ret, w, num);</p><p> return 0; } More string.h functions - strchr strchr locates the first occurrence of a character in the string. First parameter is the string to search in. Second parameter is a character to search for. The function returns the address of the first occurrence of the character found or NULL.</p><p>The syntax of this function is as follows: char* strchr(const* s, int c);</p><p>Example: #include <stdio.h> #include <string.h> int main(){ char s[31] = "Google Earth"; char *p;</p><p> p = strchr(s, 'E'); printf("character %c is found at position: %d\n", *p, p - s);</p><p> return 0; } More string.h functions - strchr </p><p> strstr function locates the first occurrence of the string in another string. First parameter is the string to search in. Second parameter is the string to search for. The function returns the address of the first occurrence of the character found or NULL.</p><p>The syntax of this function is as follows: char* strstr( const char* s, const char* subs );</p><p>Example: #include <string.h> #include <stdio.h></p><p> void find_str(char const* s, char const* subs) { char* position = strstr(s, subs); if(position) { //NULL or valid address of the fist characer found printf("found the string '%s' in '%s' at position: %ld\n", subs, s, position - s); } else { printf("the string '%s' was not found in '%s'\n", subs, s); } }</p><p> int main(void) { char* str = "Looking for a string!!"; find_str(str, "Look"); find_str(str, "g"); find_str(str, "bet"); find_str(str, "or");</p><p> return 0; } </p><p>Try it!</p><p>Write a function compare_length that will receive two strings as parameters and return 1 if the strings are not the same in length, return 0 if the strings are the same length.</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us