Comprog 1-17
Comprog 1-17
return 0; Example:
} #include <stdio.h>
#include <string.h>
Objective: Learn to dynamically allocate
and deallocate memory.
int main() {
4. Summary char source[] = "World";
char destination[20];
Recap the importance of pointers in memory
manipulation and dynamic programming.
strcpy(destination, source);
Highlight best practices: initialize pointers, printf("Copied String: %s\n", destination);
avoid dangling pointers, and free allocated return 0;
memory. }
WEEK 15 3. strcat()
1. Introduction to Strings and String Functions
Purpose: Concatenates (appends) one string to
another. int main() {
char source[] = "Hello World";
Syntax: char *strcat(char *destination, const char
char destination[6];
*source);
Example: strncpy(destination, source, 5);
destination[5] = '\0'; // Manually adding null
#include <stdio.h>
character
#include <string.h>
printf("Copied String: %s\n", destination);
return 0;
int main() {
}
char str1[20] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
4. strcmp()
Purpose: Compares two strings lexicographically.
Syntax: int strcmp(const char *str1, const char
*str2);
Return Values:
0: Strings are equal.
< 0: First string is less than the second.
> 0: First string is greater than the second.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Apple";
char str2[] = "Banana";