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

C++ Question using Strings Develop a terminal application that accomplish the fo

ID: 3682074 • Letter: C

Question

C++ Question using Strings

Develop a terminal application that accomplish the following requirements: Read a string from console. Make a function to check if all the characters of the string are unique (the string has no repeated characters) using the following algorithm: Given that all the characters arc represented using the ascii code, we can create a boolean array of 256 values where each value represents an ascii character. Initialize the array in false. Then, loop through the characters of the string and check: if the array at the character's ascii position is false, change it to true; if not, return false. Otherwise, return true. The program should show a message indicating if the characters are unique or not. Write a sentence explaining the complexity of the program using Big-O notation. Also, show this message for console.

Explanation / Answer

main.cpp

#include<iostream>
using namespace std;

//Returns string stating if string is unique or not.
string isUnique(string s){
   //There are 256 unique ascii characters hence array of size 256.
   if(s.length()>256){
       return "Not Unique!";
   }
   int arr[256]={0},x;
   for(int j=0;j<s.length();j++){
       //Convert character into ascii code.
       int ascii_char = s[j];
       if(arr[x]>0){
           return "Not Unique!";
       }
   //Increment ascii code stating that character encountered.
       arr[x]++;
   }
   return "Unique";
}

int main(){
   int t;
   string input_string;
   cin>>t;
   for(int i=0;i<t;i++){
       cin>>input_string;
       cout<<isUnique(input_string)<<endl;
   }
   return 0;
}

output:

25

5

unique

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