C++ String Reverse Application: Analysis and Vulnerabilities

Verified

Added on  2022/11/16

|10
|1216
|314
Homework Assignment
AI Summary
This homework assignment presents a C++ console application designed to reverse a user-inputted string. The program includes functions to determine the string's length and reverse its characters, with example outputs provided. The assignment delves into the program's structure, explaining the use of preprocessors, namespaces, and key functions such as `find_length()` and `reverse_string()`. It highlights the core algorithm, which involves iterating through the string and rearranging characters. Furthermore, the assignment identifies potential vulnerabilities in string manipulation within C++, such as difficulties compared to other languages, potential crashes with non-string inputs, and limitations related to string length. It proposes solutions like exception handling and adjusting memory allocation to address these issues. The conclusion emphasizes the efficiency and simplicity of C++ for string operations, while also noting potential alternative methods for string reversal, and successfully demonstrates the core concepts.
Document Page
Running head: OOPS CONCEPTS: C++
OOPs concepts: C++
Name of the Student
Name of the Course
Instructor
Date
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
1OOPS CONCEPTS: C++
String Input Console Application and Program Analysis (#1)
Introduction
C++ is an object oriented approach for programming where the name clearly suggests
that it uses objects. The C++ is an extension of C program where the OOPs concept is included.
It is developed by Bjarne stroutstrup at bell labs (Stroustrup, 2013). As it is known that OOPs
has six major characteristics named polymorphism, inheritance, abstraction, encapsulation, class
and objects. A class is consist with several objects, functions and data variables. The analysis of
advantages and concepts of using C++ along with string operation, its vulnerabilities and ideal
solutions has been done here using C++ compiler.
Program: (String Reverse)
#include <iostream>
#include <cstring>
using namespace std;
//Function will count and return the length if the string
int find_length(char string_input[]){
//Declaring the variables
int counter;
//Counting the the length of the string
for(counter=0;string_input[counter]!='\0';++counter);
Document Page
2OOPS CONCEPTS: C++
//returns the counted length
return counter;
}
//Function Will reverse the string
void reverse_string(char string_input[],int length){
//Declaring the variables
int i,counter = length - 1;
char reversed_string[100];
//Reversing the string using loops
for(i = 0;i < length;i++){
reversed_string[i] = string_input[counter];
counter--;
}
reversed_string[i] = '\0';
//Printing the reversed string
cout<<"String after reversing: "<<reversed_string;
Document Page
3OOPS CONCEPTS: C++
}
int main()
{
//Declaring the variables
char string_input[100];
int length;
//Printing the message for taking input
cout<< "Please enter a string to reverse: " << endl;
//Taking input for the string
cin>>string_input;
//calling the find_length to find the length of the string
length = find_length(string_input);
//Printing the length of the string
cout<<"\nLength of the string "<<string_input<<" is "<<length<<endl;
//Printing the entered string
cout<<"String before reversing: "<<string_input<<endl;
//calling the function to reverse the string
reverse_string(string_input,length);
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
4OOPS CONCEPTS: C++
return 0;
}
Output:
Figure 1: input string “john”
Source: created by author
Document Page
5OOPS CONCEPTS: C++
Figure 2: input string “uk”
Source: created by author
Figure 3: input string “bartholomew”
Source: created by author
Document Page
6OOPS CONCEPTS: C++
Figure 4: input string “zachariah”
Source: created by author
Discussion
The C++ mainly focuses on the data rather than the procedures. The programs are
segmented into several parts named objects where the data structure is used to categorize objects.
Starting from the top, a C++ program consists of preprocessors (#include<iostream>) where the
iostream means the input and output connectivity (Decyk, 2015). Next comes the functions for
example “int main()” that defines a type. Std::cout<< is for output and std::cin>> for taking any
inputs in the code. The assignment implements a C++ program to take a string input from user
and converts it into the reversed string. It also provides the length of the string.
Program Analysis
The program starts with the preprocessors and using namespace for using classes. Length
function is defined, the variables are declared within the function. The main logic of the function
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7OOPS CONCEPTS: C++
is to store every character of the string in an array (Savitch & Mock, 2015). For loop is used for
count the array indexes and that will give the length of the string. For the next part, that is the
string reverse that will rearrange the char arrays in descending order to obtain the reversed string.
Algorithm:
Step 1: start
Step 2: Take input for the string
Step 3: for loop for char array
Step 4: count every character of the string
Step 5: print the array indexes.
Step 6: arrange the char array in descending order of length.
Step 7: store the reversed string in another array
Step 8: print the reversed string.
Step 9: stop.
Vulnerabilities and Solutions
In the case of string manipulation in C++ it quite difficult as compared to other
languages. Though C++ has offered many library functions to work with the string still
sometimes it becomes a tedious job while working with string. This application required to deal
with string. If anyone tries to enter a non-string input, then the program will crash (Deitel &
Deitel, 2016). For avoiding this kind of issues, exception handling will help in executing the
program. With some help of try and catch block the problem can be tackaled.
Document Page
8OOPS CONCEPTS: C++
Since the maximum length of the string is to 100 characters. Therefore, the string can
hold only 100 characters. To avoid this problem, declare the string with more memory (means
number of character). It is required to take care while accessing the characters of the string.
Should not cross the string length while accessing the string. Otherwise, it will return some
garbage value.
Conclusion
In conclusion, C++ is a high-level language. It provides a programmer with many built-in
library functions to work with string and string manipulation. It supports both structural and
object-oriented programming. C++ is a simple and efficient language with less compilation time.
The implementation of the string reverse and length count has been successfully implemented
and outputs of the console application has been attached in the assignment as screenshot. There
are several methods are present for reverse like swapping, using reverse function (inbuilt library)
using character array etc. However, the use of reverse function could make the task a lot easier.
The opportunity of getting something learned from the implementation is present here. Hence, it
can be said that the C++ is very dynamic language and the implementation of the C++ program
has been done successfully.
Document Page
9OOPS CONCEPTS: C++
References
Decyk, V. K. (2015). Introduction to object-oriented concepts using Fortran90.
Deitel, P., & Deitel, H. (2016). C++ how to Program. Pearson.
Savitch, W. J., & Mock, K. (2015). Problem Solving with C++. Pearson.
Stroustrup, B. (2013). The C++ programming language. Pearson Education.
chevron_up_icon
1 out of 10
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]