Day11_Assignment
Day11_Assignment
int main() {
char str[100], clean_str[100];
printf("Enter a string: ");
scanf("%[^\n]" , str);
int flag = 0, k = 0;
if (flag == 0) {
printf("Palindrome\n");
} else {
printf("Not a Palindrome\n");
}
return 0;
}
2.
/*Problem 2: Word Frequency Counter
Problem Statement:
Write a program to count the frequency of each word in a given string.
Use strtok() to tokenize the string and strcmp() to compare words.
Ignore case differences.
Example:
Input: "This is a test. This test is simple."
Output:
Word: This, Frequency: 2
Word: is, Frequency: 2
Word: a, Frequency: 1
Word: test, Frequency: 2
Word: simple, Frequency: 1 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str[100];
char words[50][50];
int frequency[50] = {0};
int wordCount = 0;
if (!found) {
strcpy(words[wordCount], token);
frequency[wordCount] = 1;
wordCount++;
}
return 0;
}
3.
/*Problem 3: Find and Replace
Problem Statement:
Create a program that replaces all occurrences of a target substring
with another substring in a given string.
Use strstr() to locate the target substring and
strcpy() or strncpy() for modifications.
Example:
Input:
String: "hello world, hello everyone"
Target: "hello"
Replace with: "hi"
Output: "hi world, hi everyone"*/
#include <stdio.h>
#include <string.h>
int main() {
char text[100];
char target[10];
char replace[10];
char result[200];
printf("String: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0';
printf("Target: ");
scanf("%s", target);
printf("Replace: ");
scanf("%s", replace);
strcat(result, replace);
strcat(result, currentPos);
#include <stdio.h>
#include <string.h>
int main() {
char str[100], reverse_str[100];
char *tokens[50];
int token_count = 0;
printf("Input: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
reverse_str[0] = '\0';
5.
/*Problem 5: Longest Repeating Substring
Problem Statement:
Write a program to find the longest substring that appears more than
once in a given string. Use strncpy() to extract substrings and
strcmp() to compare them.
Example:
Input: "banana"
Output: "ana"*/
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int maxLen = 0;
char longestSubstr[100] = "";
int count = 0;
for (int i = 0; i <= len - (end - start); i++) {
if (strncmp(&str[i], substr, end - start) == 0) {
count++;
}
}
if (maxLen > 0) {
printf("Longest repeating substring: \"%s\"\n", longestSubstr);
} else {
printf("No repeating substring found.\n");
}
return 0;
}