Untitled

 avatar
irfann
plain_text
6 months ago
1.8 kB
0
Indexable
Never
2nd practical

#include <stdio.h>
int stringLength(const char *str)
{
 int length = 0;
 while (str[length] != '\0')
 {
 length++;
 }
 return length;
}
void stringCopy(char *dest, const char *src)
{
 int i = 0;
 while (src[i] != '\0')
 {
 dest[i] = src[i];
 i++;
 }
 dest[i] = '\0';
}
void stringReverse(char *str)
{
 int left = 0;
 int right = stringLength(str) - 1;
 while (left < right)
 {
 char temp = str[left];
 str[left] = str[right];
 str[right] = temp;
 left++;
 right--;
 }
}
int isPalindrome(const char *str)
{
 int left = 0;
 int right = stringLength(str) - 1;
 while (left < right)
 {
 if (str[left] != str[right])
 {
 return 0;
 }
 left++;
 right--;
 }
 return 1;
}
void stringConcatenate(char *dest, const char *src)
{
 int dest_len = stringLength(dest);
 int i = 0;
 while (src[i] != '\0')
 {
 dest[dest_len + i] = src[i];
 i++;
 }
 dest[dest_len + i] = '\0';
}
int countSubstringOccurrences(const char *str, const char *substr)
{
 int occurrences = 0;
 int str_len = stringLength(str);
12
 int substr_len = stringLength(substr);
 for (int i = 0; i <= str_len - substr_len; i++)
 {
 int j;
 for (j = 0; j < substr_len; j++)
 {
 if (str[i + j] != substr[j])
 {
 break;
 }
 }
 if (j == substr_len)
 {
 occurrences++;
 }
 }
 return occurrences;
}
int main()
{
 char str1[100] = "Hello, world!";
 char str2[100] = "racecar";
 char str3[100] = "My name is ";
 char str4[100] = "John Doe";
 char str5[100] = "abcabcabcabc";
 char substr[100] = "abc";
 printf("String Length of str1: %d\n", stringLength(str1));
 
 char copy[100];
 stringCopy(copy, str1);
 printf("Copy of str1: %s\n", copy);
 stringReverse(str2);
 printf("Reverse of str2: %s\n", str2);
 printf("Is str2 a palindrome? %s\n", isPalindrome(str2) ? "Yes" : "No");
 stringConcatenate(str3, str4);
 printf("Concatenated string: %s\n", str3);
}
Leave a Comment