23 Jan C Prog 2 SS

 

1. Header Files

#include <stdio.h> #include <string.h> #include <ctype.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 functions like strchr(), strcpy(), strtok(), strlen(), etc.
  • #include <ctype.h>: Includes the C Standard Library for character handling functions like toupper().

2. countOccurrences Function


int countOccurrences(char *str, char ch) { int count = 0; char *ptr = strchr(str, ch); while (ptr != NULL) { count++; ptr = strchr(ptr + 1, ch); // Continue searching after the found character } return count; }
  • int countOccurrences(char *str, char ch): This function takes a string (str) and a character (ch) as arguments and returns the number of occurrences of ch in str.
  • char *ptr = strchr(str, ch): Uses strchr() to find the first occurrence of the character ch in the string str. strchr() returns a pointer to the first occurrence or NULL if the character is not found.
  • while (ptr != NULL): The loop continues until strchr() no longer finds the character.
    • count++: Increments the count each time the character is found.
    • ptr = strchr(ptr + 1, ch): Moves the pointer ptr forward 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) { int len = strlen(str); for (int i = 0; i < len / 2; i++) { char temp = str[i]; str[i] = str[len - i - 1]; str[len - i - 1] = temp; } }
  • void reverseString(char *str): This function reverses the string str in place.
  • int len = strlen(str): Gets the length of the string using strlen().
  • for (int i = 0; i < len / 2; i++): Iterates over the first half of the string.
    • char temp = str[i]: Stores the character at position i in a temporary variable temp.
    • str[i] = str[len - i - 1]: Assigns the character at the corresponding position from the end of the string to the current position i.
    • str[len - i - 1] = temp: Assigns the character stored in temp to the position len - 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


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

5. Declare and Initialize Variables


char str1[100] = "Hello, World!"; char str2[100]; char word[20]; char sentence[200] = "C programming is powerful. String handling is fun!";
  • str1[100] = "Hello, World!": Initializes str1 with the string "Hello, World!".
  • str2[100]: Declares str2 as a character array for later use.
  • word[20]: Declares word for storing a single word (not used in the program).
  • sentence[200] = "C programming is powerful. String handling is fun!": Initializes sentence with a longer string to be used in various operations.

6. Converting String to Uppercase


printf("Original str1: %s\n", str1); for (int i = 0; str1[i]; i++) { str1[i] = toupper(str1[i]); } printf("Uppercase str1: %s\n\n", str1);
  • for (int i = 0; str1[i]; i++): Loops through each character of str1 until it reaches the null terminator \0.
  • str1[i] = toupper(str1[i]);: Converts each character of str1 to uppercase using toupper() from the ctype.h library.
  • printf("Uppercase str1: %s\n\n", str1);: Prints the modified str1 in uppercase.

7. Counting Occurrences of a Character


char charToCount = 'I'; int count = countOccurrences(sentence, charToCount); printf("The character '%c' occurs %d time(s) in the sentence.\n\n", charToCount, count);
  • char charToCount = 'I';: Declares a character variable charToCount to store the character 'I' for counting its occurrences.
  • int count = countOccurrences(sentence, charToCount);: Calls the countOccurrences() function to count how many times 'I' appears in sentence.
  • printf(): Prints the result, showing how many times 'I' occurs in the sentence.

8. Reversing a String


strcpy(str2, "Data Structures in C"); printf("Original str2: %s\n", str2); reverseString(str2); printf("Reversed str2: %s\n\n", str2);
  • strcpy(str2, "Data Structures in C");: Copies the string "Data Structures in C" into str2.
  • reverseString(str2);: Calls the reverseString() function to reverse the string str2 in place.
  • printf(): Prints the reversed string str2.

9. Tokenizing a Sentence into Words


printf("Original sentence: %s\n", sentence); char *token = strtok(sentence, " "); printf("Tokens:\n"); while (token != NULL) { printf("%s\n", token); token = strtok(NULL, " "); }
  • char *token = strtok(sentence, " ");: The strtok() function splits the sentence into tokens (words) using a space as the delimiter. It returns the first token.
  • while (token != NULL): The loop continues until strtok() returns NULL (i.e., when all tokens are processed).
    • printf("%s\n", token);: Prints each token.
    • token = strtok(NULL, " ");: Calls strtok() again with NULL as the first argument to continue tokenizing the remainder of the string.

10. Searching for a Substring


strcpy(sentence, "C programming is powerful. String handling is fun!"); char *substring = "powerful"; char *pos = strstr(sentence, substring); if (pos) { printf("\nSubstring \"%s\" found at position: %ld\n", substring, pos - sentence); } else { printf("\nSubstring \"%s\" not found.\n", substring); }
  • char *substring = "powerful";: Declares a substring "powerful" to search for in sentence.
  • char *pos = strstr(sentence, substring);: Uses strstr() to find the first occurrence of the substring in sentence. If found, it returns a pointer to the start of the substring.
  • if (pos): Checks if pos is not NULL, meaning the substring was found.
    • printf(): Prints the position of the substring within sentence by calculating the offset with pos - sentence.
    • If the substring is not found, it prints a message saying the substring was not found.

11. Return Statement


return 0;
  • 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

Popular posts from this blog

Programming Notes by Atul Kalukhe