Arrays and String Questions

2 minute read

Check if each each element in a string is unique?

Code

#include <string>
using std::string;
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std::endl;
using std::boolalpha;

bool isUniqueChars(const string &str){

		if (str.length() > 128){
			return false;
		}

		vector<bool> char_set_(128); // Vector contains true and false for 

		for (int i = 0; i < str.length(); i++){

			int value = str[i]; // Each Char has a int representation

			cout << "Value: " << value << endl;
			if (char_set_[value]){ // if true
				return false;
			}
			else{
				char_set_[value] = true;
			}
		}
		return true;
}

int main(){

    // Vector of words passed to our function to check if their are duplicate characters

    vector<string> words = {"abcde", "Devin", "Lebron", "instagram", "oowowowowow", "cvbnmpoiwersajkl"} ;
    
    for (auto word : words)
    {
        cout << word << string(": ") << boolalpha << isUniqueChars(word) <<endl;

    }
}

Output

abcde: true
Devin: true
Lebron: true
instagram: false
oowowowowow: false
cvbnmpoiwersajkl: true

Given two strings, write a method to decide if one is permutation of the other.

#include <iostream>
using std::cout;
using std::endl;
#include<string>
using std::string;
using std::boolalpha;
using std::sort;

bool check_strings(string str1, string &str2)
{
    // Check length of string
    if (str1.length() != str2.length())
    {
        return false;
    }
    // Sort strings and compare == equality
    sort(str1.begin(),str1.end());
    sort(str2.begin(),str2.end());
    // Then just compare
    if (str1 == str2){
        return true;
    } 
    else
    {
        return false;
    }
}

int main()
{
    string one, two;
    one = "lleoH";
    two = "Hello";
    // check if two strings are anagrams

    cout << "Example One: ";
    cout << boolalpha << check_strings(one, two) << endl;

    string three,four;
    three = "hello";
    four = "heilo";
    cout << "Example Two: ";
    cout << boolalpha << check_strings(three, four) << endl;

}

Output:

Example One: true
Example Two: false

Question 3 Replace spaces in String with characters/strings

#include <string>
using std::string;
#include <vector>
using std::vector;
#include <iostream>
using std::cout;
using std::endl;

string change_string(string str)
{
    //replace space with %20

    string new_string = "";
 

    for(int i = 0; i < str.length(); ++i)
    {
        if (str[i] == ' ')
        {
            // replace
            new_string += "%20";
            
        }
        else {
            // add char to new_string

            new_string += str[i];

         }
    }

    return new_string;
}
int main()

{
    string phrase = "Mr John Smith";

    cout << change_string(phrase) << endl;
}

Output:

Mr%20John%20Smith

Question 4

Question 5

Question 6

#include <string>
using std::string;
#include <vector>
#include <iostream>

using std::cout;
using std::endl;


string compress_string(string &str ){

	string compressed_string = "";

    int count = 1;
    
    for (int i = 1; i < str.length(); i++ ){

        if (str[i-1] == str[i]) {
            ++count;

        }
        else {
            compressed_string +=str[i-1]; // add char to compressed_string
            compressed_string += std::to_string(count); // add count number to string
            count = 1; // reset count
        }

    }
    // we go all the way to the end of the string length()
    compressed_string += str[str.length() - 1]; // add last char to compressed_string
    compressed_string += std::to_string(count); // add count value to compressed_string

	return compressed_string;

}

int main()
{
	string prac = "aaaaaabbbiiiiiiii";
	cout << compress_string(prac) << endl;
}

Output:

a6b3i8

Question 7

Updated: