IPC144 Library Functions

Total Page:16

File Type:pdf, Size:1020Kb

IPC144 Library Functions

IPC144 – Library Functions Agenda:

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

Review/Questions

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.

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.

The syntax of these functions is as follows: int isdigit(int);

See example: fun57.c

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.

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);

See example of using atoi() function - review fun58.c

More stdio.h functions - more scanf()????

sscanf() reads formatted input from a string.

The syntax of this function is as follows: int sscanf(const char *str, const char *format, …. )

Example 1: #include int main(){ char s1[31] = "Flight 815"; char w[31]; int num = 0; int ret = 0;

ret = sscanf(s1, "%s%d", w, &num); printf("ret is %d, w is %s, num is %d\n", ret, w, num);

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.

The syntax of this function is as follows: char* strchr(const* s, int c);

Example: #include #include int main(){ char s[31] = "Google Earth"; char *p;

p = strchr(s, 'E'); printf("character %c is found at position: %d\n", *p, p - s);

return 0; } More string.h functions - strchr

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.

The syntax of this function is as follows: char* strstr( const char* s, const char* subs );

Example: #include #include

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); } }

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");

return 0; }

Try it!

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.

Recommended publications