[go: up one dir, main page]

0% found this document useful (0 votes)
6 views5 pages

Fpractical Nocd 4

Uploaded by

Bhavika Ahire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Fpractical Nocd 4

Uploaded by

Bhavika Ahire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Tulsiramji Gaikwad-Patil College of Engineering and Technology

Wardha Road, Nagpur-441 108


NAAC A Accredited & An Autonomous Institute
+

Approved by AICTE ,New Delhi, Govt .of Maharashtra & Affiliated to RTM Nagpur

Department of Computer Science & Engineering

Practical No -4 Date

Aim: To analyze the given string by determining its length and applying string
manipulation techniques to reverse it using C programming.

Objective:To implement a C program that calculates the length of a given string and
reverses it using standard string functions.

Libraries Required:

 #include <stdio.h> – For input and output operations.


 #include <string.h> – For string manipulation functions like strlen().

Tools Required:

 C Compiler (GCC, Turbo C, MinGW, etc.)


 Text Editor/IDE (Code::Blocks, Dev-C++, Visual Studio Code, etc.)
 Debugger for troubleshooting errors
 Terminal/Command Prompt for executing the compiled program

Theory

String manipulation is a fundamental concept in C programming, where strings are


treated as character arrays terminated by a null character ('\0'). To analyze a string,
two key operations are performed:

1.Determining the Length of a String:


The length of a string is found using the strlen() function from the <string.h> library.
This function counts the number of characters in the string until it encounters the null
terminator ('\0'). The length helps in various string operations, including traversal and
manipulation.

2.Reversing a String:
Reversing a string involves swapping characters from the beginning with characters
from the end until the middle of the string is reached. This can be done using:

The strrev() function (available in some compilers).

A manual approach using a loop and swapping characters.

String Reversal Logic

A manual approach for reversing a string involves:

Compiler Practical record written & compiled by Prof. Anjali Pise(CSE) 1


1. Finding the length of the string.
2. Swapping the first character with the last, the second with the second-last, and
so on.
3. Ensuring the null character remains at the end.

Explanation of code

1. The user enters a string.


2. The strlen() function calculates its length.
3. The reverseString() function swaps characters to reverse the string.
4. The reversed string is displayed as output.

Applications of String Manipulation

 Text processing
 Data validation
 Encryption and decryption
 String pattern matching

By applying these techniques, programmers can efficiently manipulate strings in C to


solve various real-world problems.

Program

Input Code Expected output


hello 6th sem A students this is practical Enter a string: Length of the string: 44
4 Reversed string: 4 lacitcarp si siht
stneduts A mes ht6 olleh

Necessary Code

#include <stdio.h>

#include <string.h>

void reverseString(char str[]) {

int length = strlen(str);

for (int i = 0; i < length / 2; i++) {

char temp = str[i];

str[i] = str[length - i - 1];

str[length - i - 1] = temp;

Compiler Practical record written & compiled by Prof. Anjali Pise(CSE) 2


}

int main() {

char str[100];

printf("Enter a string: ");

fgets(str, sizeof(str), stdin);

str[strcspn(str, "\n")] = 0;

int length = strlen(str);

printf("Length of the string: %d\n", length);

reverseString(str);

printf("Reversed string: %s\n", str);

return 0;

Output

Compiler Practical record written & compiled by Prof. Anjali Pise(CSE) 3


Course Outcome

Course Outcome PO
CO1 PO1,PO2,(sample)

Program Specific Outcome

Course Outcome Program Specific Outcome


PSO 1 PSO2 PSO3

Program Outcome

Course Program Outcomes


Outcome

PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO 9 PO10 PO11 PO12

CO 1 1

Conclusion: The program successfully determines the length of the input string and
reverses it using a loop, demonstrating basic string manipulation in C.

Viva - Voce

1.What function is used to determine the length of a string in C?

2.What happens if you input an empty string?

3.How does the for loop reverse the string?

4.What is the purpose of rev[len] = '\0'; in the code?

5.Why do we use gets(str) to take string input, and what is its drawback?

Signature of Lab Course Coordinator

Compiler Practical record written & compiled by Prof. Anjali Pise(CSE) 4


Compiler Practical record written & compiled by Prof. Anjali Pise(CSE) 5

You might also like