File 26

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

North South University

Department of Electrical and Computer Engineering


CSE115L: Programming Language I Lab

Lab 15: To be familiar with Strings in C

Example 1: Write a C program that reads a string from an input file, reverses the string, and writes the reversed
string to an output file.

Problem Analysis: The program is designed to perform file I/O operations coupled with string manipulation. It
will open an input file (input.txt) to read a string, reverse this string in memory, and then write the reversed string
to an output file (output.txt). The process involves handling file pointers, reading content into a buffer, reversing
the string, and finally writing the output to another file.

Input variables Processing Output variables Necessary header


variables/calculations files/functions/macros
inputFile (FILE *) outputFile stdio.h
str (char []) string.h
revStr (char [])
len (int) M_SIZE

Algorithm:
1. Start the program.
2. Open the input file "input.txt" in read mode ("r").
3. If the input file does not exist or cannot be opened, display an error message and exit the program.
4. Read a string from the input file using fgets and store it in the character array str.
5. Close the input file.
6. Remove the newline character at the end of the string if it exists.
7. Call the reverseString function, passing the input string str and the output string reversedStr.
8. Open the output file "output.txt" in write mode ("w").
9. If the output file cannot be opened, display an error message and exit the program.
10. Write the reversed string reversedStr to the output file using fprintf.
11. Close the output file.
12. Display a message indicating that the reversed string has been written to the output file.
13. End the program.

Code:

1|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

Output:

Reversed string written to output.txt


If input.txt file containt: Hello madam, output.txt file will contain: madam olleh

2|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

Discussion and Conclusion: The above program demonstrates basic file handling and string operations in C,
including reading from and writing to files, and reversing a string using indexed access within an array. Proper
error checking ensures that the program gracefully handles situations like missing files or read/write errors. This
example illustrates not only file and string manipulation but also the importance of proper resource management
in C, such as closing files after their use.

Example 2: Write a C program that processes a string from an input file by first isolating and recording each
character individually, and then computing the string's length without using standard library functions. Output
both results to a single file.

Problem Analysis: To solve this problem, we will read a string from an input file and perform two tasks. These
tasks can be done simultaneously within a single loop. As we iterate through the string, character by character,
we will count each character. At the same time, we will prepare them for output by formatting them as separate
entities, with each character followed by a space. To achieve modularity and ease, we will use functions for
reading strings from files, writing strings to files, and processing strings to get the desired output and length.
Therefore, we will need input and output filenames, plus a string to pass to functions that manage these tasks.

Input variables Processing Output Necessary header


variables/calculations variables files/functions/macros
inputFilename (char []) inputFile (FILE *) stdio.h
outputFilename (char []) outputFile (FILE *)
str (char []) length (int) MAX_LEN

Algorithm:
1. Start the program.
2. Declare a character array str with a maximum length of MAX_LEN.
3. Define input and output filenames as "input.txt" and "output.txt".
4. Call the readStringFromFile function to read the string from the input file and store it in str.
5. Within the readStringFromFile function:
a. Open the input file in read mode using fopen.
b. If the input file cannot be opened, display an error message and return.
c. Use fgets to read a line of text from the input file into the str array.
d. Close the input file using fclose.
e. Return 0 to indicate successful completion of the function.
6. If reading from the input file fails in the main function, display an error message and exit the program.
7. Call the writeOutputToFile function to write the processed string and its length to the output file.
8. Within the writeOutputToFile function:
a. Open the output file in write mode using fopen.
b. If the output file cannot be opened, display an error message and return.
c. Write the string "Characters in the string:\n" to the output file using fputs.
d. Call the processString function, passing the output file and the string str.
e. Close the output file using fclose.
9. Within the processString function:
a. Initialize an integer variable length to 0.
b. While the character at index length of the string str is not null and length is less than MAX_LEN:
i. Write the character at index length of the string str to the output file using fputc.
ii. Write a space character directly after each character using fputc.
iii. Increment the length variable.
c. Write the length of the string to the output file using fprintf, along with a newline character.
3|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

10. Display a message indicating the completion of the process along with the name of the output file.
11. End the program.
Code:

4|Page
North South University
Department of Electrical and Computer Engineering
CSE115L: Programming Language I Lab

Output:
Process completed. The characters and length of the string have been written to 'output.txt'.
input.txt output.txt
Characters in the string:
Hello SIR Hello SIR

Length of the string: 10

Discussion and Conclusion: This C program reads a string from an `input.txt` file, processes it by writing each
character with a space to an `output.txt` file, and appends the string's length. It employs modular functions to
manage file operations and string processing, enhancing readability and maintainability. The program efficiently
handles I/O and string manipulation within a streamlined and structured approach.

Lab Exercises:

1. Write a C program to count the number of words and lines from a input file.
2. Write a C program to find the repeated character in a string.
3. Write a C program to check whether a substring is present in a string.
4. Write a program in C to read a sentence and replace lowercase characters with uppercase and vice versa.
5. Write a program in C to find the number of times a given word 'the' appears in the given string.
Sample Output :
Input the string : The string where the word the present more than once.
The frequency of the word 'the' is : 3
6. Write a program in C to remove characters from a string except alphabets.
Sample Output :
Input the string : w3resource.com
After removing the Output String : wresourcecom
7. Write a program in C to read a file and remove the spaces between two words of its content.
The content of the file is : The quick brown fox jumps over the lazy dog
After removing the spaces the content is : Thequickbrownfoxjumpsoverthelazydog
8. Write a C program to sort a string array in ascending order:

5|Page

You might also like