PSIPL7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Name Vatsal Panchal

UID no. 2024800084

Experiment No. 7

AIM:
To Implement various text processing problems.

Program 1

PROBLEM Write a program that generates and prints the Fibonacci words of order 0
STATEMENT : through 5. For example, f(0) = "A", f(1) = "B", f(2) = "BA", f(3) = "BAB", f(4) =
"BABBA", etc. Using string.h library functions

PROGRAM: #include <stdio.h>


#include <string.h>

int main()
{
char str[50];
char l1[50] = "A";
char l2[50] = "B";

int n;
printf("Enter a number:");
scanf("%d", &n);

int i;
for(int i=1; i<=n; i++)
{
strcpy(str, l2);
strcat(str, l1);
printf("%s\n", str);
strcpy(l1, l2);
strcpy(l2, str);
}

return 0;
}

RESULT:

Program 2

PROBLEM Write a program to convert the String “123” to Integer 123 and vice versa.
STATEMENT :

PROGRAM: #include<stdio.h>
#include<stdlib.h>

int main()
{
char intstr[] = "123";
char floatstr[] = "12.3";

int num = atoi(intstr);


long longnum = atol(intstr);
float floatnum = atof(floatstr);

printf("Using atoi: %d\n", num);


printf("Using atol: %ld\n", longnum);
printf("Using atof: %f\n", floatnum);

char buffer[50];
int day = 25, month = 11, year = 2024;
sprintf(buffer, "Date: %02d/%02d/%04d", day, month, year);
printf("Using sprintf: %s\n", buffer);

char inputstr[] = "11 22.2 Hello";


int intvalue;
double doublevalue;
char word[20];

sscanf(inputstr, "%d%lf%s", &intvalue, &doublevalue, word);


printf("Using sscanf:\n");
printf("Input str: %s\n", inputstr);
printf("Extracted integer: %d\n", intvalue);
printf("Extracted double: %.2lf\n", doublevalue);
printf("Extracted string: %s\n", word);

return 0;
}

RESULT:

Program 3

PROBLEM The names of the students are stored in a 2D array of strings. (matrix of
STATEMENT: names) Write the program to sort the names in ascending order in the matrix
using

-Any sorting algorithm

-Any string.h library function

PROGRAM: #include<stdio.h>
#include<string.h>
#define size 50
int main()
{
char students[size][size];

int n;
printf("Enter number of students:");
scanf("%d", &n);

printf("Enter students of students:\n");


for(int i=0;i<n;i++)
{
scanf("%s", students[i]);
}

for(int i=0; i<n-1;i++)


{
for(int j=0;j<n-i-1;j++)
{
if(strcmp(students[j], students[j+1])>0)
{
char temp[size];
strcpy(temp, students[j]);
strcpy(students[j], students[j+1]);
strcpy(students[j+1], temp);
}
}
}

printf("Sorted students in ascending order:\n");


for(int i=0;i<n;i++)
{
printf("%s\n", students[i]);
}
return 0;
}
RESULT:

Program 4

PROBLEM Write a program to Demonstrate the following without using the string.h library
STATEMENT: functions

● strlen
● strcpy
● strcat
● strrev
● strcmp

PROGRAM: #include<stdio.h>
#define size 100
int main()
{
char str[size];
printf("Enter something:");
scanf("%s", str);

int count=0;
while(str[count]!='\0')
{
count++;
}
printf("Length of string is %d.\n", count);

char str2[count];
for(int i=0;i<count;i++)
{
str2[i] = str[i];
}

printf("str2 = %s\n", str2);

char str3[] = "hello";


int k=0;
while(str3[k]!='\0')
{
str2[count+k] = str3[k];
k++;

}
str2[count+k] = '\0';
printf("%s\n", str2);

char str4[count];
for(int l=0;l<count;l++)
{
str4[l] = str[count-l-1];
}
printf("Reversed string = %s.\n", str4);

if(count!=5)
{
printf("%d\n", count-5);
}

return 0;
}
RESULT:

Program 5

PROBLEM Write a program that takes a string input and performs ANY THREE of the
STATEMENT: following operations in sequence:

1. Delete Repeated Words: Remove all repeated words, retaining


only the first occurrence.
2. Find and Replace: Replace all occurrences of a specified word
with a new word.
3. Count Characters, Words, Vowels, and Consonants: Output
the total counts of characters, words, vowels, and consonants.
4. Identify Palindromes: Identify and output all palindromic words
in the string.
5. Find Maximum Occurring Character/Word: Output the
character and word that appears most frequently in the string.

Sample Input - Output:


Input String: Madam, I am Adam. I am certain that my deed is level, for my
deed is a deed of honor.
Word to Find: Adam
Word to Replace: Eve

Output:
1. String After Removing Repeated Words: Madam, I am Adam. certain that
my deed is level, for honor.
2. String After Find and Replace: Madam, I am Eve. certain that my deed is
level, for honor.
3. Counts:
- Total Characters: 75
- Total Words: 13
- Vowels: 18
- Consonants: 30

4. Palindromic Words in String: Madam deed level


5. Maximum Occurring Character/Word: "deed" (appears 3 times)

PROGRAM: #include <stdio.h>


#include <string.h>

void deleteRepeatedWords(char input[], char output[])


{
char words[100][50] = {0};
int wordCount = 0;
char temp[50];
int i, j, k = 0, isRepeated;

for (i = 0; input[i] != '\0'; i++)


{
if (input[i] == ' ' || input[i] == '.' || input[i] == ',')
{
if (k > 0)
{
temp[k] = '\0';
isRepeated = 0;

for (j = 0; j < wordCount; j++)


{
if (strcmp(temp, words[j]) == 0)
{
isRepeated = 1;
break;
}
}
if (!isRepeated)
{
strcpy(words[wordCount++], temp);
}
k = 0;
}
}
else
{
temp[k++] = input[i];
}
}
if (k > 0)
{
temp[k] = '\0';
isRepeated = 0;
for (j = 0; j < wordCount; j++)
{
if (strcmp(temp, words[j]) == 0)
{
isRepeated = 1;
break;
}
}
if (!isRepeated)
{
strcpy(words[wordCount++], temp);
}
}

output[0] = '\0';
for (i = 0; i < wordCount; i++)
{
strcat(output, words[i]);
if (i < wordCount - 1)
{
strcat(output, " ");
}
}
}

void findAndReplace(char input[], char findWord[], char replaceWord[],


char output[])
{
char buffer[1000] = "";
char temp[50];
int i, k = 0;

for (i = 0; input[i] != '\0'; i++)


{
if (input[i] == ' ' || input[i] == '.' || input[i] == ',')
{
if (k > 0)
{
temp[k] = '\0';
if (strcmp(temp, findWord) == 0)
{
strcat(buffer, replaceWord);
}
else
{
strcat(buffer, temp);
}
strcat(buffer, " ");
k = 0;
}
}
else
{
temp[k++] = input[i];
}
}
if (k > 0)
{
temp[k] = '\0';
if (strcmp(temp, findWord) == 0)
{
strcat(buffer, replaceWord);
}
else
{
strcat(buffer, temp);
}
}
strcpy(output, buffer);
}

int main()
{
char inputstr[] = "Madam, I am Adam. I am certain that my deed is level,
for my deed is a deed of honor.";
char outputstr[100];

printf("Original String:\n%s\n", inputstr);

deleteRepeatedWords(inputstr, outputstr);
printf("After deleting repeated words:\n%s\n", outputstr);

char findword[] = "Adam";


char replaceword[]= "Eve";
char outputstr2[50];

findAndReplace(inputstr,findword,replaceword,outputstr2);
printf("Replaced sring:\n%s\n", outputstr2);

char vowel[] = "AEIOUaeiou";


char consonant[] =
"BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz";

int countcharacters = 0, countvowels = 0, countconsonants = 0;

for (int i = 0; i < strlen(outputstr2); i++)


{
if (outputstr2[i] != ' ')
countcharacters++;
}
printf("Total Characters: %d\n", countcharacters);

for (int i = 0; i < strlen(outputstr2); i++)


{
for (int j = 0; j < strlen(vowel); j++)
{
if (outputstr2[i] == vowel[j])
{
countvowels++;
break;
}
}
}
printf("Total Vowels: %d\n", countvowels);
for (int i = 0; i < strlen(outputstr2); i++)
{
for (int j = 0; j < strlen(consonant); j++)
{
if (outputstr2[i] == consonant[j])
{
countconsonants++;
break;
}
}
}
printf("Total Consonants: %d\n", countconsonants);

return 0;
}

RESULT:

CONCLUSION: In this experiment we learned about the string library and different
functions of it. We learned how to use to use different string functions to
make the code for efficient and easy to read.

You might also like