01
String Functions (<string.h>)
Include <string.h> to use these functions.
Copy
strcpy(destination, source) // copy source into destination
strncpy(target, source, n) // copy at most n characters
Concatenation
strcat(str1, str2) // append str2 to end of str1
strncat(str1, str2, n) // append at most n characters
Comparison
strcmp(str1, str2) // returns: <0 if str1<str2, 0 if equal, >0 if str1>str2
stricmp(str1, str2) // compare, ignoring case
strncmp(str1, str2, n) // compare at most n characters
Case
strlwr(str) // convert to lowercase
strupr(str) // convert to uppercase
Length and Other
strlen(str) // return length (not counting '\0')
strrev(str) // reverse the string in place
strdup(str) // return a duplicate
strchr(str, c) // pointer to first occurrence of c
strset(str, ch) // set all characters to ch
strnset(str, ch, n) // set first n characters to ch
02
Math Functions (<math.h>) and Character Functions (<ctype.h>)
Mathematical Functions — include <math.h>
| Function | Syntax | Description |
|---|---|---|
abs() | int abs(int num) | Absolute value of integer |
fabs() | double fabs(double num) | Absolute value of float |
ceil() | double ceil(double num) | Smallest integer ≥ num (round up) |
floor() | double floor(double num) | Largest integer ≤ num (round down) |
fmod() | double fmod(double x, double y) | Remainder of x/y (float modulus) |
pow() | double pow(double base, double exp) | base raised to exp |
sqrt() | double sqrt(double num) | Square root (num must be ≥ 0) |
Character Functions — include <ctype.h>
| Function | Tests/Does |
|---|---|
isalnum(ch) | True if letter or digit |
isalpha(ch) | True if letter |
isdigit(ch) | True if digit (0–9) |
islower(ch) | True if lowercase letter |
isupper(ch) | True if uppercase letter |
ispunct(ch) | True if punctuation (not space) |
isspace(ch) | True if whitespace |
tolower(ch) | Returns lowercase version |
toupper(ch) | Returns uppercase version |
Conversion Functions — include <stdlib.h>
| Function | Description |
|---|---|
atof(str) | Convert string to double |
atoi(str) | Convert string to int |
atol(str) | Convert string to long int |
itoa(num, str, radix) | Convert int to string in given base |
03
Practice Exercises — Lesson 7
04
Try It: String, Char & Math
Experiment with other math.h functions like ceil(), floor(), or log(). Try changing the string too.