Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me with my code, I\'m using Microsoft Visual Studio 2017. #incl

ID: 3846999 • Letter: C

Question

Can someone help me with my code, I'm using Microsoft Visual Studio 2017.

#include <iostream>
#include <string>
#include <iomanip>
#include<stdio.h>
#include<ctype.h>

//function prototypes
void getString(char*, char*);
int getLength(char*);
int isPalindrome(char*);
void displayReverse(char*);
void concat(char*, char*);
void displayResults(char*, char*);

int main()
{
   //declare two char array to hold string
   char str1[250], str2[250];

   //create pointers to the strings
   char *firstString = str1;
   char *secondString = str2;
   int flag = 1;
   char choice;

   //repeat the below execution until the user wishes to quit
   do
   {
       //get the input using getString method
       getString(firstString, secondString);
       //display the results using displayResults method
       displayResults(firstString, secondString);
       //prompt and read if user wishes to repeat
       printf(" Would you like to continue with another pair of strings (Y of N)? ");
       scanf(" %c", &choice);
       //if user enters n exit the loop
       if (choice == 'n' || choice == 'N')
       {
           flag = 0;
       }
       getchar();
   } while (flag == 1);

   return 0;
}

//function that takes two pointer variables and read the strings using pointers
void getString(char *firstString, char *secondString)
{
   //prompt and read two strings
   printf("Enter the first string: ");
   gets(firstString);
   printf("Enter the second string: ");
   gets(secondString);
}

//function that takes a pointer to the string as parameter and returns the length of the string
int getLength(char *str)
{
   int len = 0;
   //loop through the string and get its length
   while (*str != '')
   {
       len++;
       str++;
   }
   return len;
}

//function that takes a pointer to string and checks if string is palindrome or not
int isPalindrome(char *str)
{
   //get the length of the string
   int len = getLength(str);
   int isPalin = 1;
   int i;
   //loop through the string and check if string is palindrome or not
   for (i = 0; i<len; i++)
   {
       if (tolower(str[i]) != tolower(str[len - i - 1]))
           isPalin = 0;
   }
   return isPalin;
}

//function that takes a pointer to string and prints the reverse of the string
void displayReverse(char *str)
{
   //get the length of the string
   int len = getLength(str);
   int i;
   //starting from the last position print the characters in the string in reverse order
   for (i = len; i >= 0; i--)
       printf("%c", *(str + i));
}

//function that takes two string and print the concatenated string
void concat(char *firstString, char *secondString)
{
   int i = 0, j = 0;
   char concatenated[500];
   char *newString = concatenated;

   //loop through the first string and add it to the new string
   while (firstString[i] != '')
   {
       newString[j] = firstString[i];
       j++;
       i++;
   }

   i = 0;
   //loop through the second string, and add it to the new string
   while (secondString[i] != '')
   {
       newString[j] = secondString[i];
       j++;
       i++;
   }
   newString[j] = '';
   //print the concatenated string
   for (i = 0; i<j; i++)
       printf("%c", *(newString + i));
}

//function that takes two string and displays the results
void displayResults(char *firstString, char *secondString)
{
   printf(" Length of the first string is %d", getLength(firstString));
   printf(" Length of the second string is %d", getLength(secondString));
   if (isPalindrome(firstString))
       printf(" First string is a palindrome");
   else
       printf(" First string is not palindrome");
   if (isPalindrome(secondString))
       printf(" Second string is a palindrome");
   else
       printf(" Second string is not palindrome");
   printf(" Reverse of the first string is: ");
   displayReverse(firstString);
   printf(" Revese of the second string is: ");
   displayReverse(secondString);
   printf(" The concatenated string is: ");
   concat(firstString, secondString);
}

//function that takes two pointer variables and read the strings using pointers 51 52 void getString (char first String, char secondstring) //prompt and read two strings 54 printf "Enter the first string: ")i 55 ets (first string); 56 printf "Enter the second string: 57 gets secondstring) 58 59 60 61 //function that takes a pointer to the string as parameter and returns the length of the string 62 int get Length char str) 100 Error List (X) 1 Error 40 warnings 0 Messages xr Build IntelliSense Entire Solution Line suppression State File Code Description Project identifier "gets" is

Explanation / Answer

#include <iostream>
#include <string>
#include <iomanip>
#include<stdio.h>
#include<ctype.h>

//function prototypes
void getString(char*, char*);
int getLength(char*);
int isPalindrome(char*);
void displayReverse(char*);
void concat(char*, char*);
void displayResults(char*, char*);

int main()
{
   //declare two char array to hold string
   char str1[250], str2[250];

   //create pointers to the strings
   char *firstString = str1;
   char *secondString = str2;
   int flag = 1;
   char choice;

   //repeat the below execution until the user wishes to quit
   do
   {
       //get the input using getString method
       getString(firstString, secondString);
       //display the results using displayResults method
       displayResults(firstString, secondString);
       //prompt and read if user wishes to repeat
       printf(" Would you like to continue with another pair of strings (Y of N)? ");
       scanf(" %c", &choice);
       //if user enters n exit the loop
       if (choice == 'n' || choice == 'N')
       {
           flag = 0;
       }
       getchar();
   } while (flag == 1);

   return 0;
}

//function that takes two pointer variables and read the strings using pointers
void getString(char *firstString, char *secondString)
{
   //prompt and read two strings
   printf("Enter the first string: ");
   gets(firstString);
   printf("Enter the second string: ");
   gets(secondString);
}

//function that takes a pointer to the string as parameter and returns the length of the string
int getLength(char *str)
{
   int len = 0;
   //loop through the string and get its length
   while (*str != '')
   {
       len++;
       str++;
   }
   return len;
}

//function that takes a pointer to string and checks if string is palindrome or not
int isPalindrome(char *str)
{
   //get the length of the string
   int len = getLength(str);
   int isPalin = 1;
   int i;
   //loop through the string and check if string is palindrome or not
   for (i = 0; i<len; i++)
   {
       if (tolower(str[i]) != tolower(str[len - i - 1]))
           isPalin = 0;
   }
   return isPalin;
}

//function that takes a pointer to string and prints the reverse of the string
void displayReverse(char *str)
{
   //get the length of the string
   int len = getLength(str);
   int i;
   //starting from the last position print the characters in the string in reverse order
   for (i = len; i >= 0; i--)
       printf("%c", *(str + i));
}

//function that takes two string and print the concatenated string
void concat(char *firstString, char *secondString)
{
   int i = 0, j = 0;
   char concatenated[500];
   char *newString = concatenated;

   //loop through the first string and add it to the new string
   while (firstString[i] != '')
   {
       newString[j] = firstString[i];
       j++;
       i++;
   }

   i = 0;
   //loop through the second string, and add it to the new string
   while (secondString[i] != '')
   {
       newString[j] = secondString[i];
       j++;
       i++;
   }
   newString[j] = '';
   //print the concatenated string
   for (i = 0; i<j; i++)
       printf("%c", *(newString + i));
}

//function that takes two string and displays the results
void displayResults(char *firstString, char *secondString)
{
   printf(" Length of the first string is %d", getLength(firstString));
   printf(" Length of the second string is %d", getLength(secondString));
   if (isPalindrome(firstString))
       printf(" First string is a palindrome");
   else
       printf(" First string is not palindrome");
   if (isPalindrome(secondString))
       printf(" Second string is a palindrome");
   else
       printf(" Second string is not palindrome");
   printf(" Reverse of the first string is: ");
   displayReverse(firstString);
   printf(" Revese of the second string is: ");
   displayReverse(secondString);
   printf(" The concatenated string is: ");
   concat(firstString, secondString);
}