0% found this document useful (0 votes)
5 views7 pages

Programs

Uploaded by

sruthikamurugan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views7 pages

Programs

Uploaded by

sruthikamurugan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

BIT - CODE VITA’23

DEPARTMENT OF TRAINING AND PLACEMENT


&
SPECIAL LABORATORIES
DAY - 2 - ASSESSMENT QUESTIONS

EASY:
1.Remove the non alphabet character:
#include <stdio.h>
int main()
{
char str[100];
int i, j;
printf("Sample Input: ");
fgets(str,sizeof(str),stdin);
for(i = 0; str[i] != '\0'; ++i)
{
while (!((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '\0'||
str[i]=='"' ))
{
for(j = i; str[j] != '\0'; ++j)
{
str[j] = str[j+1];
}
str[j] = '\0';
}
}
printf("Sample Output: %s", str);
return 0;
}
2.Remove the white spaces
#include <stdio.h>
#include <string.h>

int main() {
int i, j;
char str[100];
printf("Sample Input: ");
scanf("%[^\n]", str);
int len = strlen(str);
for (i = 0; i < len; i++) {
if (str[i] == ' ') {
for (j = i; j < len; j++) {
str[j] = str[j + 1];
}
len--;
i--;
}
}
printf("Sample output: %s", str);
return 0;
}
INTERMEDIATE:
1.Character Removal:
#include <stdio.h>
#include <string.h>

void removeChar(char *str, char *c) {


int i, j;
int len = strlen(str);
int toRemove = strlen(c);

for (i = j = 0; i < len; i++) {


int found = 0;
for (int k = 0; k < toRemove; k++) {
if (str[i] == c[k]) {
found = 1;
break;
}
}
if (!found) {
str[j++] = str[i];
}
}
str[j] = '\0';
}

int main() {
char str[100], c[100];
printf("Sample Input:\n");
printf("input_str = ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
printf("characters_to_remove = ");
fgets(c, sizeof(c), stdin);
c[strcspn(c, "\n")] = '\0';

removeChar(str, c);

printf("Sample Output:\n");
printf("%s\n", str);
return 0;
}
2. Finding the first repetitive and last repetitive character in a string:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void findFirstLastRepetitiveChar(const char *str) {
int indices[256];
for (int i = 0; i < 256; i++) {
indices[i] = -1;
}
char first_char = '\0';
char last_char = '\0';
for (int i = 0; str[i] != '\0'; i++) {
char current = str[i];
if (isalpha(current)) {
if (indices[current] == -1) {
indices[current] = i;
} else {
last_char = current;
if (first_char == '\0') {
first_char = current;
}
}
}
}
if (first_char != '\0') {
int first_index = indices[first_char];
int last_index = indices[last_char];
printf("Output:\n");
printf("First repeating alphabet character: '%c' (at index %d)\n", first_char,
first_index);
printf("Last repeating alphabet character: '%c' (at index %d)\n", last_char,
last_index);
} else {
printf("No repetitive alphabet characters found in the string.\n");
}
}
int main() {
char str[100];
printf("Input: ");
scanf("%s", str);
findFirstLastRepetitiveChar(str);
return 0;
}
HARD:
1. String Palindromic Substrings without Pre built functions:
#include <stdio.h>
#include <string.h>
int isPalindrome(const char *str, int start, int end)
{
while (start < end) {
if (str[start] != str[end])
{
return 0;
}
start++;
end--;
}
return 1;
}
void findPalindromicSubstrings(const char *str) {
int len = strlen(str);
printf("[");
for (int i = 0; i < len; i++)
{
for (int j = i; j < len; j++)
{
if (isPalindrome(str, i, j))
{
for (int k = i; k <= j; k++)
{
printf("%c", str[k]);
}
printf(",");
}
}
}
printf("]");
}
int main()
{
char input[100];
printf("Enter a string: ");
scanf("%s", input);
printf("Palindromic substrings:");
findPalindromicSubstrings(input);
return 0;
}

2.String Tokenization without Pre built functions:


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

void tokenizeString(char input_string[], char *tokens[], int *token_count) {


char *token = strtok(input_string, " ");

while (token != NULL) {


char cleaned_token[100];
int cleaned_token_index = 0;

for (int i = 0; i < strlen(token); i++) {


if (isalpha(token[i])) {
cleaned_token[cleaned_token_index++] = token[i];
}
}
cleaned_token[cleaned_token_index] = '\0';

if (cleaned_token[0] != '\0') {
tokens[(*token_count)++] = strdup(cleaned_token);
}

token = strtok(NULL, " ");


}
}

int main() {
char input_string[1000];
printf("Sample Input: ");
fgets(input_string, sizeof(input_string), stdin);

char *tokens[100];
int token_count = 0;

tokenizeString(input_string, tokens, &token_count);


printf("Sample Output:");
if (token_count > 1) {
printf("[");
}
for (int i = 0; i < token_count; i++) {
if (i > 0) {
printf(", ");
}

printf("\"%s\"", tokens[i]);
free(tokens[i]);
}
if (token_count > 1) {
printf("]");
}
printf("\n");

return 0;
}

You might also like