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

Please help me with this c++ code. I need to print out the number of digits,tota

ID: 3701622 • Letter: P

Question

Please help me with this c++ code. I need to print out the number of digits,total characters, and number of letter using pointer. In particular, I'm having a hard time initializing the character array. I have my code here.

#include <iostream>
#include <string>

using namespace std;

void getStatistics(char sentence[], int* noOfChars,int* noOfDigits,int* noOfAlphabets);

int main()
{
int noOfChars;
int noOfDigits;
int noOfAlphabets;
string sentence = "";
int stringLen = 0;
cout<< "Enter a sentence to analyze:";
cin >> sentence;
  
char* sen[] = new char[sentence.length()-1];
  
for(int i = 0; i < sentence.length()-1; i++){
sen[i] = &sentence[i];
}
getStatistics(sen, &noOfChars,&noOfDigits,&noOfAlphabets);
  
cout<< noOfChars << endl;
cout<< noOfDigits << endl;
cout<< noOfAlphabets << endl;
  
  
return 0;
}

void getStatistics(char sentence[], int* noOfChars,int* noOfDigits,int* noOfAlphabets){
  
for(int i =0; i < sizeof(sentence)/sizeof(sentence[0]);i++){
if((sentence[i] >= 97 && sentence[i] <= 122) || (sentence[i] >= 65 && sentence[i] <= 90)){
noOfAlphabets++;
  
}
if((sentence[i] >= 48 && sentence[i] <= 57)){
noOfDigits++;
  
}
noOfChars++;
}
}

I'm not entirely sure why is doesn't run, but I need the header to remain as it is. Please help me, and thank you.

Explanation / Answer

#include<iostream>

#include<cstring>
#include<cstdlib>

using namespace std;

void getStatistics(char sentence[], int* noOfChars,int* noOfDigits,int* noOfAlphabets);

int main()
{
   int noOfChars = 0 ;
   int noOfDigits = 0;
   int noOfAlphabets = 0;
   string sentence = "";
   cout<< "Enter a sentence to analyze :: ";
   cin >> sentence;
   int stringLen = sentence.size() + 1;
  
   char * sen = new (nothrow) char[stringLen];
   for(int i = 0; i < stringLen ;i++) {
   sen[i] = sentence[i];
   }
   getStatistics(sen, &noOfChars,&noOfDigits,&noOfAlphabets);

   cout << endl << "======================================================" << endl;
   cout << "String Statistics are mentioned below" << endl;
   cout << "======================================================" << endl;
   cout << "Alphabets in the sentence are :: " << noOfAlphabets << endl;
   cout << "Digits in the sentence are :: " << noOfDigits << endl;
   cout << "Total Characters in the sentence are :: " << noOfChars << endl << endl;
   delete [] sen;

   return 0;
}

void getStatistics(char sentence[], int* noOfChars,int* noOfDigits,int* noOfAlphabets){
  
   for (int i = 0; sentence[i] != ''; i++){
   if((sentence[i] >= 97 && sentence[i] <= 122) || (sentence[i] >= 65 && sentence[i] <= 90)){
   *noOfAlphabets = *noOfAlphabets + 1;
   }
   else if((sentence[i] >= 48 && sentence[i] <= 57)){
   *noOfDigits = *noOfDigits + 1;
   }
   *noOfChars = *noOfChars + 1;
   }

}
/******************* Output of Program ************************
Enter a sentence to analyze :: Hithere123,HowAreYouToday!!!

======================================================
String Statistics are mentioned below
======================================================
Alphabets in the sentence are :: 21
Digits in the sentence are :: 3
Total Characters in the sentence are :: 28

**************************************************************/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote