23 Jan C Prog 1 SS
1. Header Files
#include <stdio.h>: Includes the Standard Input/Output library to enable input/output operations likeprintf().#include <string.h>: Includes the String library to provide string manipulation functions likestrlen(),strcpy(),strcat(), etc.
2. Main Function
- The
main()function is the entry point of the C program.
3. Declaring Strings
str1,str2,str4are initialized with strings"Hello, World!","Welcome to C programming.", and"C is fun!", respectively.str3is declared as a character array but is not initialized at this point.
4. strlen - Find the Length of a String
strlen(str1): Thestrlen()function returns the length of the stringstr1(excluding the null-terminating character).- It prints the length of
str1andstr2using the format specifier%zufor size_t, the return type ofstrlen().
5. strcpy - Copy One String Into Another
strcpy(str3, str1): Copies the contents ofstr1intostr3. After this,str3will hold"Hello, World!".- The
printf()statement prints the copied stringstr3.
6. strcat - Concatenate Two Strings
strcat(str1, " "): Concatenates a space (" ") to the end ofstr1, changing it to"Hello, World! ".strcat(str1, str4): Then, concatenatesstr4("C is fun!") tostr1, resulting in"Hello, World! C is fun!".- The program prints the concatenated string.
7. strcmp - Compare Two Strings
strcmp(str1, str2): Comparesstr1andstr2lexicographically. It returns:0if the strings are equal,- a positive value if
str1is lexicographically greater thanstr2, - a negative value if
str1is lexicographically less thanstr2.
- The program checks the result and prints whether
str1andstr2are equal, or if one is greater or less than the other.
8. strchr - Find the First Occurrence of a Character
strchr(str2, 'C'): Searches for the first occurrence of the character'C'instr2. If found, it returns a pointer to the character; otherwise, it returnsNULL.- The program calculates the position of
'C'instr2by subtracting the base address ofstr2frompos, and prints the position.
9. strstr - Find the First Occurrence of a Substring
strstr(str2, "programming"): Searches for the substring"programming"withinstr2. 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): Fills the first 5 bytes ofstr3with the character'*'. After this,str3becomes"*****".- The null terminator
\0is 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): Copies the first 7 characters fromstr2intostr3. After this,str3will contain"Welcome".- A null terminator is manually added at position 7.
- The program prints the copied string
str3.
12. strtok - Tokenize a String
strtok(str5, " "): Thestrtok()function splits the stringstr5into tokens based on the delimiter" "(space in this case). It returns a pointer to the first token.- The
whileloop continues callingstrtok()withNULLas the first argument to tokenize the remaining parts of the string. - Each token is printed on a new line.
13. Return Statement
- The
main()function returns0to indicate that the program has executed successfully.
14. Program End
- Marks the end of the
main()function.
Comments
Post a Comment