Reversing a C-string in C++ using a for loop, Lab Reports of Object Oriented Programming

A C++ code that defines a function to reverse a C-string (an array of char) using a for loop that swaps the first and last characters, then the second and next-to-last character and so on. The code also includes a main function that gets a string from the user, calls the reverse function and displays the reversed string. the full code and comments explaining each step.

Typology: Lab Reports

2022/2023

Available from 12/14/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
OOPs Lab Lab # 11 1
Lab Task 11.1 Write a function reverse() that reverses a C-string (an array of char). Use a for loop that
swaps the first and last characters, then the second and next-to-last character and so on. Pass a string
from main() to reverse() as an argument to reverse it.
#include <iostream>
#include <cstring> //for strlen()
using namespace std;
int main()
{
void reversit( char[] ); //prototype
const int MAX = 80; //array size
char str[MAX]; //string
cout << "\nEnter a string: "; //get string from user
cin.get(str, MAX);
reversit(str); //reverse the string
cout << "Reversed string is: "; //display it
cout << str << endl;
return 0;
}
//--------------------------------------------------------------
//reversit()
//function to reverse a string passed to it as an argument
void reversit( char s[] )
{
int len = strlen(s); //find length of string
for(int j = 0; j < len/2; j++) //swap each character
{ // in first half
char temp = s[j]; // with character
s[j] = s[len-j-1]; // in second half
s[len-j-1] = temp;
}
}
pf2

Partial preview of the text

Download Reversing a C-string in C++ using a for loop and more Lab Reports Object Oriented Programming in PDF only on Docsity!

OOPs Lab Lab # 11 1

Lab Task 11.1 Write a function reverse() that reverses a C-string (an array of char). Use a for loop that swaps the first and last characters, then the second and next-to-last character and so on. Pass a string from main() to reverse() as an argument to reverse it. #include #include //for strlen() using namespace std; int main() { void reversit( char[] ); //prototype const int MAX = 80; //array size char str[MAX]; //string cout << "\nEnter a string: "; //get string from user cin.get(str, MAX); reversit(str); //reverse the string cout << "Reversed string is: "; //display it cout << str << endl; return 0; } //-------------------------------------------------------------- //reversit() //function to reverse a string passed to it as an argument void reversit( char s[] ) { int len = strlen(s); //find length of string for(int j = 0; j < len/2; j++) //swap each character { // in first half char temp = s[j]; // with character s[j] = s[len-j-1]; // in second half s[len-j-1] = temp; } }

OOPs Lab Lab # 11 2

Lab Task 11.2 Write a function compare() that will take two strings as arguments from main(). Compare these two strings whether they are equal or one is equal than other.

#include

using namespace std;

void relationalOperation(string s1, string s2)

if (s1 != s2)

cout << s1 << " is not equal to "

<< s2 << endl;

if (s1 > s2)

cout << s1 << " is greater than "

<< s2 << endl;

else

cout << s2 << " is greater than "

<< s1 << endl;

// Main function

int main()

system("color f0");

string s1("Geeks");

string s2("forGeeks");

relationalOperation(s1, s2);

return 0;