PSIPL7
PSIPL7
PSIPL7
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
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";
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);
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
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);
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];
}
}
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:
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
output[0] = '\0';
for (i = 0; i < wordCount; i++)
{
strcat(output, words[i]);
if (i < wordCount - 1)
{
strcat(output, " ");
}
}
}
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];
deleteRepeatedWords(inputstr, outputstr);
printf("After deleting repeated words:\n%s\n", outputstr);
findAndReplace(inputstr,findword,replaceword,outputstr2);
printf("Replaced sring:\n%s\n", outputstr2);
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.