23 Jan C Prog 2 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 functions likestrchr(),strcpy(),strtok(),strlen(), etc.#include <ctype.h>: Includes the C Standard Library for character handling functions liketoupper().
2. countOccurrences Function
int countOccurrences(char *str, char ch): This function takes a string (str) and a character (ch) as arguments and returns the number of occurrences ofchinstr.char *ptr = strchr(str, ch): Usesstrchr()to find the first occurrence of the characterchin the stringstr.strchr()returns a pointer to the first occurrence orNULLif the character is not found.while (ptr != NULL): The loop continues untilstrchr()no longer finds the character.count++: Increments the count each time the character is found.ptr = strchr(ptr + 1, ch): Moves the pointerptrforward to continue searching from the next character after the current one.
return count;: Returns the total count of occurrences.
3. reverseString Function
void reverseString(char *str): This function reverses the stringstrin place.int len = strlen(str): Gets the length of the string usingstrlen().for (int i = 0; i < len / 2; i++): Iterates over the first half of the string.char temp = str[i]: Stores the character at positioniin a temporary variabletemp.str[i] = str[len - i - 1]: Assigns the character at the corresponding position from the end of the string to the current positioni.str[len - i - 1] = temp: Assigns the character stored intempto the positionlen - i - 1, effectively swapping the characters.
- The loop continues until it reaches the middle of the string, reversing the string in place.
4. main Function
- The
main()function is the entry point of the program.
5. Declare and Initialize Variables
str1[100] = "Hello, World!": Initializesstr1with the string"Hello, World!".str2[100]: Declaresstr2as a character array for later use.word[20]: Declareswordfor storing a single word (not used in the program).sentence[200] = "C programming is powerful. String handling is fun!": Initializessentencewith a longer string to be used in various operations.
6. Converting String to Uppercase
for (int i = 0; str1[i]; i++): Loops through each character ofstr1until it reaches the null terminator\0.str1[i] = toupper(str1[i]);: Converts each character ofstr1to uppercase usingtoupper()from thectype.hlibrary.printf("Uppercase str1: %s\n\n", str1);: Prints the modifiedstr1in uppercase.
7. Counting Occurrences of a Character
char charToCount = 'I';: Declares a character variablecharToCountto store the character'I'for counting its occurrences.int count = countOccurrences(sentence, charToCount);: Calls thecountOccurrences()function to count how many times'I'appears insentence.printf(): Prints the result, showing how many times'I'occurs in the sentence.
8. Reversing a String
strcpy(str2, "Data Structures in C");: Copies the string"Data Structures in C"intostr2.reverseString(str2);: Calls thereverseString()function to reverse the stringstr2in place.printf(): Prints the reversed stringstr2.
9. Tokenizing a Sentence into Words
char *token = strtok(sentence, " ");: Thestrtok()function splits thesentenceinto tokens (words) using a space as the delimiter. It returns the first token.while (token != NULL): The loop continues untilstrtok()returnsNULL(i.e., when all tokens are processed).printf("%s\n", token);: Prints each token.token = strtok(NULL, " ");: Callsstrtok()again withNULLas the first argument to continue tokenizing the remainder of the string.
10. Searching for a Substring
char *substring = "powerful";: Declares a substring"powerful"to search for insentence.char *pos = strstr(sentence, substring);: Usesstrstr()to find the first occurrence of the substring insentence. If found, it returns a pointer to the start of the substring.if (pos): Checks ifposis notNULL, meaning the substring was found.printf(): Prints the position of the substring withinsentenceby calculating the offset withpos - sentence.- If the substring is not found, it prints a message saying the substring was not found.
11. Return Statement
return 0;: Returns 0 to indicate that the program has executed successfully.
12. End of main Function
- Marks the end of the
main()function.
Comments
Post a Comment