Need help with printing out theASCII representaion of an integer and the hexadec
ID: 3758757 • Letter: N
Question
Need help with printing out theASCII representaion of an integer and the hexadecimal representation of a character at a given index. These are just two parts of many I have to do out of a template that i will post. I have the other functions done up to the GetIntegerValueOfIndex and PrintHexValueAtIndex functions. If possibe could you show the output (screenshot) of your code. The last several answers I got on previous questions did not compile correctly for me. Sample output for the GetIntegerValueOfIndex function and PrintHexValueAtIndex function provided for me are:
Sample output for PrintHexValueAtIndex: Sample output for GetIntegerValueOfIndex:
Enter a string less than 80 characters. Enter a string less than 80 characters.
abcdABCD abcdABCD
HEX=61 a=97
HEX=62 b=98
HEX=63 c=99
HEX=64 d=100
HEX=41 A=65
HEX=42 B=66
HEX=43 C=67
HEX=44 D=68
Template:
#include <iostream>
// Function Prototypes=========================================================
// Do NOT change the signature of these function prototypes.
// If you do, your code will not compile with my altered main() function
// I suggest that you copy the prototypes below and then fill them in.
// ----------------------------------------------------------------------------
// Read in a line of text INCLUDING SPACES into a string.
// You may assume that the input will NOT exceed the maxLength available.
// Keep in mind that cin stops reading at a whitespace. See page 318.
void ReadString(char * c, int maxLength);
// Get the length of the string and store it in length
// Hint: How does C++ terminate a string? Look for that character!
void GetStringLength(char * c, int * length);
// PrintString - Just print the string in forward order using cout
void PrintString(char * const c);
// PrintStringBackwards - Print the string in reverse order using cout
void PrintStringBackwards(char * const c);
// Search the string for the test character. Return -1 if not found
int FindIndexOfCharacter(char * c, char testVal);
// Return a pointer to the character at the index given
char * GetValueAtIndex(char * const c, int index);
// Return the ascii integer value of the character at requested index
int GetIntegerValueOfIndex(char * c, int index);
// Print the hexadecimal value of the character at the requested index
void PrintHexValueAtIndex(char * c, int index);
// Make the entire string uppercase
void MakeUpperCase(char * c);
// Make the entire string lowercase
void MakeLowerCase(char * c);
// ============================================================================
//[BEGIN MAIN] -- Don't delete this line
int main()
{
// Use these variables to test.
// SIZE could change so make sure your code works with different sizes.
const int SIZE = 80;
char ca[SIZE];
char * pc = ca;
int fPrints = 0;
int bPrints = 0;
int lengthChecks = 0;
// Your code below
// =========================
// =========================
// Your code above
std::cout << "Press ENTER";
std::cin.get();
return 0;
}
//[END MAIN] -- Don't delete this line
void ReadString(char * c, int maxLength)
{
std::cout << "Enter a string less than " << maxLength << " characters." << std::endl;
// Your code here
// ==============================
// ==============================
}
void GetStringLength(char * c, int * length)
{
}
// You need to provide the rest of the functions
// See Function Prototypes at the top.
Thank You in Advance!!!
Explanation / Answer
Program:
// conversion.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include<cstring>
using namespace std;
void ReadString(char * c, int maxLength);
void GetStringLength(char * c, int * length);
void PrintString(char * const c);
void PrintStringBackwards(char * const c);
char * GetValueAtIndex(char * const c, int index);
int GetIntegerValueOfIndex(char * c, int index);
void PrintHexValueAtIndex(char * c, int index);
void MakeUpperCase(char * c);
void MakeLowerCase(char * c);
int lengthChecks = 0;
int main()
{
const int SIZE=80;
char ca[SIZE];
char *pc=ca;
int fPrints = 0;
int bPrints = 0;
int maxLength;
cout << "Enter string length: ";
cin >> maxLength;
cout << endl;
ReadString( ca, maxLength);
int * length = 0;
string s1;
GetStringLength( pc, length);
char x1=0;
PrintString(pc);
char x = 0;
PrintStringBackwards(pc);
int index = 0;
GetValueAtIndex(pc, index);
GetIntegerValueOfIndex(pc, index);
PrintHexValueAtIndex(pc, index);
MakeUpperCase(pc);
MakeLowerCase(pc);
cout << "Press ENTER";
cin.get();
system("pause");
return 0;
}
void ReadString(char * c, int maxLength)
{
while(*c!='')
{
lengthChecks++;
c++;
}
void GetStringLength(char * c, int * length)
{
while(length>0)
{
cout << *(c+length);
}
}
void PrintString(char * const c)
{
char s1[80];
int i;
for(i=0; s1[i]!=''; ++i);
cout << s1[i];
}
void PrintStringBackwards(char * const c)
{
//=======================
char s1[20];
if (cin.getline(s1, sizeof s1)) // prints the string in reverse
{
for (size_t i = strlen(s1) - 1; i != -1; i--)
cout<< s1[i];
cout << s1[0] << std::endl;
}
}
char * GetValueAtIndex(char * const c, int index)
{
return &c[index]; //return the address of array c at index
}
int GetIntegerValueOfIndex(char * c, int index)
{
cin >> index;
cout << "DEC= " << dec << (int)c << endl;
return 0;
}
void PrintHexValueAtIndex(char * c, int index)
{
cin >> index;
cout << "HEX= " << hex << (int)c << endl;
}
void MakeUpperCase(char * c)
{
int i;
c=str[i];
putchar(toupper(c));
}
void MakeLowerCase(char *c)
{
string str;
int i;
while (c==str[i])
{
putchar(tolower(c));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.