int function2 (const char * string1) returning the number of unique digits appearing in the string, e.g. function2 ("ab512af6kc1") -> 3.
int function2(const char* string1) {
int zero = 0, one = 0, two = 0, three = 0, four = 0, five = 0, six = 0,
seven = 0, eight = 0, nine = 0, counter = 0;
for (int i = 0; i < strlen(string1); i++) {
if (string1[i] == '0') {
zero++;
}
if (string1[i] == '1') {
one++;
}
if (string1[i] == '2') {
two++;
}
if (string1[i] == '3') {
three++;
}
if (string1[i] == '4') {
four++;
}
if (string1[i] == '5') {
five++;
}
if (string1[i] == '6') {
six++;
}
if (string1[i] == '7') {
seven++;
}
if (string1[i] == '8') {
eight++;
}
if (string1[i] == '9') {
nine++;
}
}
if (zero == 1) {
counter++;
}
if (one == 1) {
counter++;
}
if (two == 1) {
counter++;
}
if (three == 1) {
counter++;
}
if (four == 1) {
counter++;
}
if (five == 1) {
counter++;
}
if (six == 1) {
counter++;
}
if (seven == 1) {
counter++;
}
if (eight == 1) {
counter++;
}
if (nine == 1) {
counter++;
}
return counter;
}
It's every correct in this code, but it's long a bit. Could someone help me and write SHORTER code? It's the only way that I can measure up to this exercise.
You can use an array instead of 10 variables. Calculate the index in the array by converting the character to an integer.
int function2(const char *in) {
// Array to hold digits occurence counts.
unsigned digits[10]{};
// Iterate over the characters in input.
// Better (auto i : std::string_view(in)) in C++17.
for (auto i = in; *i; ++i) {
if (isdigit(*i)) {
// Increment the proper digit index.
digits[*i - '0']++;
}
}
int count = 0;
// Go through digit occurences.
for (auto i : digits) {
// If the digit occurred only once.
if (i == 1) {
// Increment the count.
count++;
}
}
return count;
}
To shorten your code, use an array instead of 10 individual variables:
int digits[10] = {0}; // instead of int zero = 0, one = 0, ...
To check whether a char is a representation of a digit, use isdigit:
if (isdigit(string1[i])) // instead of if (string1[i] == '0'), if (string1[i] == '1'), ...
The only non-trivial part is to convert a char to the corresponding int:
string1[i] - '0'
This code subtracts the character code of 0 (usually 48) from the character code of a digit (usually 49 for 1, 50 for 2, ..., 57 for 9). The result is an index to your array of counters.
So, to increase the proper array element, use the following code:
digit = string1[i] - '0';
digits[digit]++; // instead of zero++, one++, ...
After the code goes over the input string, count the number of digits which appeared once:
int counter = 0;
for (digit = 0; digit < 10; ++digit)
{
if (digits[digit] == 1)
++counter;
}
Use a hash table collection class to keep track of unique digits. In this case, unordered_set will do just fine. Don't even bother converting the char to integer. You're just looking for unique chars between '0' and '9'.
#include <string>
#include <unordered_set>
size_t getUniqueDigits(const std::string& string1)
{
std::unordered_set<char> table;
for (char c : string1)
{
if ((c >= '0') && (c <= '9'))
{
table.insert(c);
}
}
return table.size();
}
A more traditional "C" based solution that doesn't use any std:: collections or objects is to use an array to be that "set"
int getUniqueDigits(const char* string1)
{
int table[10] = {0};
int count = 0;
const size_t len = (string1 != nullptr) ? strlen(string1) : 0;
for(size_t i = 0; i < len; i++)
{
char c = string1[i];
if ((c >= '0') && (c <= '9'))
{
table[c - '0'] = 1;
}
}
for (char j = '0'; j <= '9'; j++)
{
count += table[j];
}
return count;
}
Just use an ordinary array as for example in this demonstrative program
#include <iostream>
size_t unique_digits( const char *s )
{
unsigned char digits[10] = { 0 };
for ( ; *s; ++s )
{
if ( '0' <= *s && *s <= '9' )
{
if ( digits[*s - '0'] != 2 ) ++digits[*s - '0'];
}
}
size_t count = 0;
for ( unsigned char c : digits ) count += c == 1;
return count;
}
int main()
{
std::cout << unique_digits( "ab512af6kc1" ) << '\n';
return 0;
}
The program output is
3
Or you can declare the array of the element type size_t. In this case the function will look the following way
#include <iostream>
size_t unique_digits( const char *s )
{
size_t digits[10] = { 0 };
for ( ; *s; ++s )
{
if ( '0' <= *s && *s <= '9' )
{
++digits[*s - '0'];
}
}
size_t count = 0;
for ( unsigned char c : digits ) count += c == 1;
return count;
}
int main()
{
std::cout << unique_digits( "ab512af6kc1" ) << '\n';
return 0;
}
I think you already have many good solutions.
Here is mine version anyway
int function2(const char* string1) {
int count[10] = {0};
int counter = 0;
int i;
for(i = 0; i < strlen(string1); i++)
int a = (++count[string1[i]-'0']);
if(a == 1)counter++;
if(a == 2)counter--;
return counter;
}
I haven't tried it. Hope there is no error
Edit:
I tried it. It seems to work fine now.
Related
Our teacher gave us this exercise:
"Given a string like '-5,14' write a function that returns the float value of -5,14
I used double here just to test the precision, but it also didn't work with floats.
[also i'm from Europe, we use the comma instead of the dot. Oh also we aren't allowed to use the type string and bool, we have to "make" them like in C]
This is what i came up with, and it seems to work a little bit. Positive numbers are similar, but wrong, and given a negative number, the result is similar to 10 times the positive of the given number.
It should work like this:
I read the string into an array of characters;
I check if the first character is a minus. if so, subtract 1 from the number of integer figures because i will count them later starting from index 0;
I count the number of integer figures with a loop from the start of the array to the ',' character;
I count the number of decimal figures with a loop from after the ',' to the end of the string;
[Keep in mind for the next step that, following the ASCII table, the code for the character of a number is that number + 48]
I add to the result variable every integer figure multiplied by ten to the power of whatever place in the number it has.
I do the same for the deicmal values but with the negative exponent.
if the number was negative, i multiply the result with -1.
But for some reason it's not working properly. The lower the number is, the less accurate it is (given 4,5 the result is 9, but given 345,543 the result is 350,43)
#include <iostream>
#define EOS '\0'
#define DIM 100
#define TRUE 1
#define FALSE 0
void leggiN(char* c)
{
std::cout << "Insert a number: ";
std::cin >> c;
}
double stof(char* str)
{
double Result = 0;
double ascii_to_int = 48;
int i = 0;
int j = 0;
int IntegerDigits = 0;
int DecimalDigits = 0;
int CommaIndex;
int isNegative = FALSE;
if (str[0] == '-')
{
IntegerDigits = -1;
isNegative = TRUE;
}
while (str[i] != ',')
{
++IntegerDigits;
++i;
}
CommaIndex = i;
++i;
while (str[i] != EOS)
{
++DecimalDigits;
++i;
}
for (i = (CommaIndex - 1); i >= 0; --i)
{
Result += (str[i] - ascii_to_int) * (std::pow(10, j));
++j;
}
j = 0;
for (i = (CommaIndex + 1); str[i] != EOS; ++i)
{
Result += (str[i] - ascii_to_int) * (std::pow(10, -j));
++j;
}
if (isNegative == 1)
Result = Result * -1;
return Result;
}
int main()
{
char str[DIM];
leggiN(str);
std::cout << stof(str);
}
use j = 1 to start your second for loop. You are trying to raise 10 to the power of -0
j = 1;
for (i = (CommaIndex + 1); str[i] != EOS; ++i)
{
Result += (str[i] - ascii_to_int) * (std::pow(10, -j));
++j;
}
If your code return 9.0 when you enter "4,5", your problem has nothing to do with imprecision.
There are other problems in your code, I've tried to un it and got a SEGFAULT...
#include <iostream>
#define EOS '\0' // 0 being such a special value, there is no need to
// define a named constant for it.
#define DIM 100
#define TRUE 1 // the language defines boolean values, avoid defining
#define FALSE 0 // unnecessary named constants for something that already
// exists.
void leggiN(char* c)
{
std::cout << "Insert a number: ";
std::cin >> c; // Inserting from cin to a char* is a BIG no-no.
// some compilers won't even allow it, for good reasons
// i.e.: what is the length of the array pointed to?
}
double stof(char* str) // you are indicating that you may modify str?
{
double Result = 0;
double ascii_to_int = 48; // this is a terrible name.
int i = 0;
int j = 0;
int IntegerDigits = 0;
int DecimalDigits = 0;
int CommaIndex;
int isNegative = FALSE;
if (str[0] == '-') // is str a valid pointer? what happens if NULL ??
{
IntegerDigits = -1;
isNegative = TRUE;
// you fail to skip the sing character, should have ++i here.
}
while (str[i] != ',') // what happens if there is no ',' in the string?
{ // you should check for str[i] == 0.
++IntegerDigits;
++i;
}
CommaIndex = i;
++i;
while (str[i] != EOS)
{
++DecimalDigits; // why do you count decimal digits?
++i; // you do not use this result anyway...
}
for (i = (CommaIndex - 1); i >= 0; --i)
{
// what happens if you have non-digit characters? they participate
// in the conversion??
// you call std::pow(), but do not include <cmath> at the top of the file.
// isn't str[i] - '0' clearer ?
Result += (str[i] - ascii_to_int) * (std::pow(10, j));
++j;
}
j = 0;
for (i = (CommaIndex + 1); str[i] != EOS; ++i)
{
Result += (str[i] - ascii_to_int) * (std::pow(10, -j));
++j;
}
if (isNegative == 1) // you had defined constants fot this, but don't use them.
Result = Result * -1;
return Result;
}
int main()
{
char str[DIM];
leggiN(str);
std::cout << stof(str);
}
Here is one way to achieve what you want.
#include <iostream>
#include <string>
const char DECIMAL_POINT = ','; // we'll use a named constant here....
// usually, we'd have to check the locale
// for regional specific information.
// works like atod(), conversion stops at end of string of first illegal character.
double stof(const char* str) {
// check input, must be not null, not empty
if (!str || str[0] == 0)
return 0;
int i = 0;
bool isNegative = false;
// take care of leading sign
if (str[0] == '-' || str[0] == '+') {
isNegative = (str[0] == '-');
++i;
}
// convert integer part.
double result = 0;
while ('0' <= str[i] && str[i] <= '9') {
result = (result * 10) + (str[i] - '0');
++i;
}
// only do decimals if they are there.
if (str[i] != DECIMAL_POINT)
return (isNegative) ? -result : result;
++i; // skip decimal point
double decimals = 0;
double multiplier = .1;
while ('0' <= str[i] && str[i] <= '9') {
decimals += (str[i] - '0') * multiplier;
++i;
multiplier *= .1;
}
result += decimals;
return (isNegative) ? -result : result;
}
int main() {
// always use std::string to read strings from cin.
std::string str;
std::cout << "Insert a number: ";
std::cin >> str;
std::cout << "in: " << str << " out: " << stof(str.c_str()) << '\n';
return 0;
}
I cant get the char search to work. The substring function is working but the char search won't provide the right location of the char it is looking for
#include<iostream>
#include <string>
using namespace std;
int charsearch(string searchInto, char ch, int start = 0)
{
int x = 0;
long n = searchInto.length();
for (int i = 1; i < n; i++)
{
cout << ch;
if (searchInto[i] == ch)
{
i = x;
}
else
i++;
}
cout<< x;
return x;
}
int substr(string src, string tosearch, int start = 0)
{
string searchInto = src;
long n = searchInto.size();
long m = tosearch.size();
int ans = -1;
for (int i = start; i < n; ++i)
{
int p = i;
int q = 0;
bool escape = false;
while (p < n && q < m) {
if (searchInto[p] == tosearch[q]) {
if (tosearch[q] == '/' && !escape) {
++q;
} else {
++p; ++q;
}
escape = false;
} else if (!escape && tosearch[q] == '*') {
++q;
while (q < m && p < n && searchInto[p] != tosearch[q]) ++p;
escape = false;
} else if (!escape && tosearch[q] == '?') {
++p; ++q;
escape = false;
} else if (tosearch[q] == '/' && !escape) {
escape = true;
++q;
} else break;
}
if (q == m) {
return i;
}
if (q == m - 1 && tosearch[q] == '*') {
if (q > 0 && tosearch[q - 1] == '/') continue;
else return i;
}
}
return ans;
}
int main()
{
string searchInto, tosearch;
cout<< "Enter string:";
getline(cin, searchInto);
cout << "Looking for :";
getline(cin, tosearch);
if (tosearch.length() < 2)
{
char ch = tosearch.at(0);
cout << "Found at: " <<charsearch(searchInto, ch) << endl;
cout << "Used Char" << endl;
}
else
cout << "Found at: " <<substr(searchInto, tosearch) << endl;
return 0;
}
To find a character in a string, you have two interfaces.
std::string::find will return the position of a character you find:
auto pos = yourStr.find('h');
char myChar = yourStr[pos];
If the character does not exist, then std::string::npos will be returned as the std::size_t returned for position.
stl algorithm std::find, in header algorithm returns an iterator:
auto it = std::find(yourStr.begin(), yourStr.end(), 'h');
char myChar = *it;
If the character does not exist, then it == yourStr.end().
There are some silly mistakes in your CharSearch method. First of all, You have to break the loop when you got your target character. And most importantly you are not assigning x when you are finding the target. Furthermore, there is extra increment of value i inside the loop. I have modified the function. Please check it below
int charsearch(string searchInto, char ch, int start = 0) {
int x = -1;
long n = searchInto.length();
for (int i = start; i < n; i++)
{
cout << ch;
if (searchInto[i] == ch)
{
x = i; // previously written as i = x which is wrong
break; // loop should break when you find the target
}
}
cout<< x;
return x;
}
Please note that,you can either also use find method of string or std::find of algorithm to search in string.
You need to make changes as per this code
int charsearch(string searchInto, char ch, int start = 0)
{
int x = -1; // : change, if return -1, means not found
long n = searchInto.length();
for (int i = start; i < n; i++) // : change
{
cout << ch;
if (searchInto[i] == ch)
{
x = i; // : change
break; // : change
}
}
cout<< x;
return x;
}
Note : This function will return 1st match.
I'm working on the Next Palindrome problem on SPOJ http://www.spoj.com/problems/PALIN/. My code works fine on my own machine, but SPOJ gives me SIGABRT. I'm using C++ 4.9.2
"A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Input
The first line contains integer t, the number of test cases. Integers K are given in the next t lines.
Output
For each K, output the smallest palindrome larger than K."
#include<iostream>
#include<vector>
using namespace std;
// turn 9 to 10
void round(vector<int> &input,int index) {
int len = input.size();
input[index] = 0;
input[len-index-1] = 0;
// if it is the first digit, add 1 in the front
if (index == 0) {
input.insert(input.begin(),1);
}
else {
input[index-1] ++;
input[len-index] ++;
}
}
// find the next palindrome
int palin(vector<int> &input) {
int len = input.size();
bool large = true;
bool small = true;
bool eqal = true;
// if it is a single digit
if (len == 1) {
if (input[0] == 9) {
input[0] = 11;
}
else {
input[0] ++;
}
return 1;
}
// start from the one before the middle
int index = len / 2 - 1;
while (index >= 0) {
len = input.size();
// the number supposed to be the same as input[index]
int rfl = len-index-1;
// keep record for if the updated number is smaller/equal to the original
if (input[index] > input[rfl]) {small = false; eqal = false;}
else if (input[index] < input[rfl]) {large = false; small = true; eqal = false;}
else {small = false;}
if (input[index] == 10) {round(input,index);}
else {
input[rfl] = input[index];
}
index --;
};
// restart from the one before the middle
index = (int)input.size() / 2 - 1;
// unless all digits on the left are larger than right/the more left digits are larger but some closer to the middle are smaller or equal, increase the number
if (!large || small || eqal) {
len = input.size();
if (len % 2 == 1) { // odd
if (input[index+1] == 9) {
round(input,index+1);
}
else {input[index+1] ++;}
}
else { // even
if (input[index] == 9) {
round(input,index);
}
else {
input[index-1] ++; input[index + 1] ++;
}
}
// go over the digits again to make sure it is a palindrome
while (index >= 0) {
if (input[index] == 10) {
round(input,index);
}
input[input.size()-index-1] = input[index];
index --;
}
}
return 0;
}
int main() {
int count; // how many numbers are there
cin >> count;
string buffer; // temporary to store each line of input
for (int j=0;j<count;++j) {
vector<int> number;
cin >> buffer;
if (cin.fail() || buffer.size() == 0) { // not a number or length==0
return 1;
}
for (int k=0;k<(int)buffer.size();k++) {
int temp = buffer[k] - '0'; // convert ASCII to int
number.push_back(temp); // construct vector
}
palin(number);
for (int i=0;i<(int)number.size();i++) {
cout << number[i];
}
cout << endl;
}
return 0;
}
Honestly, a brute force method would be inefficient, but would be pretty clear to code. Here, I just keep iterating through numbers until I find a palindrome, for each of the numbers:
http://coliru.stacked-crooked.com/a/2c7ac595d7c2cfa7
#include <iostream>
#include <stack>
#include <string>
#include <vector>
int main() {
int count; // how many numbers are there
std::cin >> count;
std::string buffer;
//two vectors to store output
std::vector<long int> input;
std::vector<long int> output;
//take the inputs
for(int i = 0; i < count; ++i) {
std::cin >> buffer;
input.push_back(std::stoi(buffer));
buffer.clear();
}
//check that we have the inputs
for(auto it : input) {
std::cout << it << std::endl;
}
//lambda to test for palindromes
auto is_palindrome = [](long int n) {
auto str = std::to_string(n);
std::stack<char> stack;
//Load each character into the stack
for(auto it : str) {
stack.push(it);
}
//Use the property of a stack to take out in a reverse order
for(size_t i = 0; !stack.empty(); stack.pop()) {
if (stack.top() != str[i])
return false;
else
++i;
}
return true;
};
//test for the palindromes; iterate
for(auto it : input) {
int n;
for (n = it+1; ; ++n) {
if(is_palindrome(n))
break;
else
continue;
}
output.push_back(n);
}
//output the outputs
for(auto it : output) {
std::cout << "next palindrome: " << it << '\n';
}
return 0;
}
I am trying to implement the function stoi() in c++. I have made an int array arr to store the integer ASCII of all elements of char_arr. This works fine if I print the values from my char_arr array because its a character array. But, how do I transfer my integer values from the char array to an int array and print only the numbers and not their ASCII?
Code:
int stoi(){
int *arr = new int [strlen(char_arr)];
for (int i=0; char_arr[i]!='\0'; ++i){
arr[i] = char_arr[i];
}
for (int i=0; char_arr[i] != '\0'; ++i){
if (arr[i] >= 48 && arr[i] <= 57){
cout << char_arr[i];
}
}
}
First of all, remove the first loop and use char_arr directly. You don't need to hold ints to make it work.
As for printing int values, you can use this:
for (int i = 0; char_arr[i] != '\0'; ++i) {
if (char_arr[i] >= '0' && char_arr[i] <= '9') { //I would suggest you to use this syntax instead of raw ASCII codes.
cout << (char_arr[i] - '0');
}
}
int stoi(){
/* if you do not use arr.
int *arr = new int[strlen(char_arr)];
for (int i = 0; char_arr[i] != '\0'; ++i){
arr[i] = char_arr[i];
}
*/
int sign = 1, value = 0;
if (*char_arr == '+') {
++char_arr;
}
else if (*char_arr == '-') {
++char_arr;
sign = -1;
}
while (*char_arr) {
if (*char_arr >= '0' && *char_arr <= '9') {
value = value * 10 + *char_arr - '0';
++char_arr;
} else {
break;
}
}
return sign * value;
}
Here's the one I came up with:
#include <cstdio>
#include <cstring>
#define _BASE_ 10
int main(int argc, char **argv)
{
char ascii[] = "474927";
signed int value = 0;
signed int ascii_len = strlen(ascii);
int pos = 0;
for(signed int i = ascii_len-1; i >= 0; i--)
{
if(i == 0 && ascii[i] == '-')
{
value *= -1;
continue;
}
int base = 1;
if(pos > 0)
{
base = _BASE_;
for(int j = 1; j < pos; j++)
base *= _BASE_;
}
value += base * (ascii[i] - 48);
pos++;
}
printf("Value: %d\n", value);
return 0;
}
Given 2 strings, design a function that can check whether they are rotations to each other without making any changes on them ? The return value is boolean.
e.g ABCD, ABDC, they are not rotations. return false
ABCD, CDAB or DABC are rotations. return true.
My solution:
shift one of them to right or left one position and then compare them at each iteration.
If they are not equal at all iterations, return false. Otherwise, return true.
It is O(n). Are there other more efficient solutions ?
What if the contents of them cannot be changed ?
thanks
Concatenate the given string with the given string.
Search for the target string in the concatenated string.
Example:
Given = CDAB
After step 1, Concatenated = CDABCDAB
After step 2, Success CDABCDAB
^^^^
Rather than shifting one of them, it might be more efficient to use two index variables. Start one at 0 each time and the other at each of the possible positions (0 to N-1) and increment it mod N.
If you can't modify the strings, just take the first character of string1 and compare it to each character of string2. When you get a match, compare the second char of string1 to the next char of string2, and so on.
Pseudocode:
len = strlen(string1);
len2 = strlen(string2);
if( len != len2 )
printf("Nope.");
for( int i2=0; i2 < len; i2++ ) {
for( int i1=0; i1<len; i1++ ) {
if( string1[i1] != string2[(i2+i1)%len] )
break;
}
if( i1 == len ) {
print("Yup.");
break;
}
}
A simple one would be:
(s1+s1).find(s2) != string::npos && s1.size() == s2.size();
#include <iostream>
#include <cstring>
#include<string>
using namespace std;
void CompareString(string, string, int);
int ComputeStringLength(string str);
int main()
{
string str = ""; string str1 = ""; int len = 0, len1 = 0;
cout << "\nenter string ";
cin >> str;
cout << "\nenter string 2 to compare:- ";
cin >> str1;
len = ComputeStringLength(str);
len1 = ComputeStringLength(str1);
if (len == len1)
CompareString(str, str1, len);
else
cout << "rotation not possible";
getchar();
return 0;
}
int ComputeStringLength(string str)
{
int len = 0;
for (int i = 0; str[i] != '\0'; i++)
{
len++;
}
return len;
}
void CompareString(string str, string str1, int n)
{
int index = 0, flag = 0, curr_index = 0, count1 = 0, flagj = 0;
for (int i = 0; i<n; i++)
{
for (int j = flagj; j<n; j++)
{
if (str[i] == str1[j])
{
index = j;
flagj =j;
count1++;
flag++;
if (flag == 1)
{
curr_index = index;
}
break;
}
}
}
int temp = count1;
if (count1 != n)
{
if (curr_index>=0)
{
int k = 0;
for (int i = n - 1; i>n - curr_index - 1; i--)
{
if (str[i] == str1[k])
{
temp++;
k++;
}
}
}
if (temp == n)
{
cout << "\n\nstring is same after rotation";
}
else
{
cout << "\n\nstring is not same after rotation";
}
}
else
{
cout << "\n\nstring is same after rotation";
}
}
https://dsconceptuals.blogspot.in/2016/10/a-program-to-check-if-strings-are.html