2017-02-15 3 views
-3

Я пытаюсь найти Если строка имеет все уникальные символы, а ниже - мой код, но я получаю ошибку «недопустимые типы» char [int] 'для индекса массива "в если заявление функции Unique полукокса, может кто-нибудь сказать мне, как исправить этуЧтобы проверить, есть ли строка со всеми уникальными символами в C++

#include <iostream> 
#include<cstring> 

using namespace std; 
bool unique_char(char); 

int main() 
{ 
    char s; 
    bool check; 
    cout << "Enter any string" << endl; 
    cin>>s; 
    check = unique_char(s); 
    if(check) 
     cout<<"there are no duplicates"; 
    else 
     cout<<"the string has duplicates"; 

    return 0; 
} 

// The if statement in this section has the error 
bool unique_char(char s) 
{ 
    bool check[256] = {false}; 
    int i=0; 
    while (s != '\0') 
    { 
     if (check **[(int) s[i]]**) 
      return false; 
     else 
     { 
     check[(int) s[i]] = true; 
     i++; 
     } 

    } 
} 
+0

Сколько персонажей вы считаете подходящим в одиночном 'char'? –

+0

Переменная типа 'std :: string', из заголовка' ', может содержать текстовую строку произвольной длины. –

ответ

0

Вам нужно передать массив символов, а не один символ.

int main() 
{ 
    char s[1000]; // max input size or switch to std::string 
    bool check; 
    cout << "Enter any string" << endl; 
    cin>>s; 
    check = unique_char(s); 
    if(check) 
     cout<<"there are no duplicates"; 
    else 
     cout<<"the string has duplicates"; 

    return 0; 
} 

bool unique_char(char* s) 
{ 
    bool check[256] = {false}; 
    int i=0; 
    while (s[i] != '\0') 
    { 
     if (check[(int) s[i]]) 
      return false; 
     else 
     { 
     check[(int) s[i]] = true; 
     i++; 
     } 
    } 
    return true; 
} 
Смежные вопросы