23 Jan C Prog 1 SS

 

1. Header Files


#include <stdio.h> #include <string.h>
  • #include <stdio.h>: Includes the Standard Input/Output library to enable input/output operations like printf().
  • #include <string.h>: Includes the String library to provide string manipulation functions like strlen(), strcpy(), strcat(), etc.

2. Main Function


int main() {
  • The main() function is the entry point of the C program.

3. Declaring Strings


char str1[100] = "Hello, World!"; char str2[100] = "Welcome to C programming."; char str3[100]; char str4[100] = "C is fun!";
  • str1, str2, str4 are initialized with strings "Hello, World!", "Welcome to C programming.", and "C is fun!", respectively.
  • str3 is declared as a character array but is not initialized at this point.

4. strlen - Find the Length of a String


printf("Length of str1: %zu\n", strlen(str1)); printf("Length of str2: %zu\n\n", strlen(str2));
  • strlen(str1): The strlen() function returns the length of the string str1 (excluding the null-terminating character).
  • It prints the length of str1 and str2 using the format specifier %zu for size_t, the return type of strlen().

5. strcpy - Copy One String Into Another


strcpy(str3, str1); printf("After copying, str3: %s\n\n", str3);
  • strcpy(str3, str1): Copies the contents of str1 into str3. After this, str3 will hold "Hello, World!".
  • The printf() statement prints the copied string str3.

6. strcat - Concatenate Two Strings


strcat(str1, " "); strcat(str1, str4); printf("After concatenation, str1: %s\n\n", str1);
  • strcat(str1, " "): Concatenates a space (" ") to the end of str1, changing it to "Hello, World! ".
  • strcat(str1, str4): Then, concatenates str4 ("C is fun!") to str1, resulting in "Hello, World! C is fun!".
  • The program prints the concatenated string.

7. strcmp - Compare Two Strings


int result = strcmp(str1, str2); if (result == 0) { printf("str1 and str2 are equal.\n\n"); } else if (result > 0) { printf("str1 is greater than str2.\n\n"); } else { printf("str1 is less than str2.\n\n"); }
  • strcmp(str1, str2): Compares str1 and str2 lexicographically. It returns:
    • 0 if the strings are equal,
    • a positive value if str1 is lexicographically greater than str2,
    • a negative value if str1 is lexicographically less than str2.
  • The program checks the result and prints whether str1 and str2 are equal, or if one is greater or less than the other.

8. strchr - Find the First Occurrence of a Character


char *pos = strchr(str2, 'C'); if (pos) { printf("Character 'C' found in str2 at position: %ld\n\n", pos - str2); } else { printf("Character 'C' not found in str2.\n\n"); }
  • strchr(str2, 'C'): Searches for the first occurrence of the character 'C' in str2. If found, it returns a pointer to the character; otherwise, it returns NULL.
  • The program calculates the position of 'C' in str2 by subtracting the base address of str2 from pos, and prints the position.

9. strstr - Find the First Occurrence of a Substring


char *subpos = strstr(str2, "programming"); if (subpos) { printf("Substring \"programming\" found at position: %ld\n\n", subpos - str2); } else { printf("Substring \"programming\" not found.\n\n"); }
  • strstr(str2, "programming"): Searches for the substring "programming" within str2. If found, it returns a pointer to the first occurrence of the substring.
  • The position is calculated similarly as in the previous step, and the program prints the result.

10. memset - Fill Memory with a Constant Byte


memset(str3, '*', 5); str3[5] = '\0'; // Null-terminate the string printf("After memset, str3: %s\n\n", str3);
  • memset(str3, '*', 5): Fills the first 5 bytes of str3 with the character '*'. After this, str3 becomes "*****".
  • The null terminator \0 is manually added at position 5 to ensure the string is properly terminated.
  • The program prints the modified string str3.

11. strncpy - Copy a Specific Number of Characters


strncpy(str3, str2, 7); str3[7] = '\0'; // Null-terminate the string printf("After strncpy, str3: %s\n\n", str3);
  • strncpy(str3, str2, 7): Copies the first 7 characters from str2 into str3. After this, str3 will contain "Welcome".
  • A null terminator is manually added at position 7.
  • The program prints the copied string str3.

12. strtok - Tokenize a String


char str5[100] = "Tokenize this string into words."; char *token = strtok(str5, " "); printf("Tokens:\n"); while (token) { printf("%s\n", token); token = strtok(NULL, " "); }
  • strtok(str5, " "): The strtok() function splits the string str5 into tokens based on the delimiter " " (space in this case). It returns a pointer to the first token.
  • The while loop continues calling strtok() with NULL as the first argument to tokenize the remaining parts of the string.
  • Each token is printed on a new line.

13. Return Statement


return 0;
  • The main() function returns 0 to indicate that the program has executed successfully.

14. Program End


}
  • Marks the end of the main() function.

Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe