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 ofch
instr
.char *ptr = strchr(str, ch)
: Usesstrchr()
to find the first occurrence of the characterch
in the stringstr
.strchr()
returns a pointer to the first occurrence orNULL
if 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 pointerptr
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)
: This function reverses the stringstr
in 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 positioni
in 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 intemp
to 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!"
: Initializesstr1
with the string"Hello, World!"
.str2[100]
: Declaresstr2
as a character array for later use.word[20]
: Declaresword
for storing a single word (not used in the program).sentence[200] = "C programming is powerful. String handling is fun!"
: Initializessentence
with 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 ofstr1
until it reaches the null terminator\0
.str1[i] = toupper(str1[i]);
: Converts each character ofstr1
to uppercase usingtoupper()
from thectype.h
library.printf("Uppercase str1: %s\n\n", str1);
: Prints the modifiedstr1
in uppercase.
7. Counting Occurrences of a Character
char charToCount = 'I';
: Declares a character variablecharToCount
to 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 stringstr2
in place.printf()
: Prints the reversed stringstr2
.
9. Tokenizing a Sentence into Words
char *token = strtok(sentence, " ");
: Thestrtok()
function splits thesentence
into 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 withNULL
as 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 ifpos
is notNULL
, meaning the substring was found.printf()
: Prints the position of the substring withinsentence
by 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