count char elements from vector string

Hi Guys Im trying to check if the string have all the same characters or not
if it is same i want to print it out, can some one tell me whats wrong below


void func()
{
vector<string> word={"loop", "aaaaa"} ;

int count;

for(int i= 0; i<word.size(); ++i)
{
cout<<"\n ";

for(int j=0; j<word[i].size(); ++j)
{
cout<<word[i][j] ;

char s=word[i][0];

int count = 0, looper = word[i].size() ;


while(looper!=0)
{
if(word[i][j]==s)
{
count ++;
}

looper --;
}

}

cout<<count;

if(count == word[i].size())
{
cout<<"all word same" ; //i want aaaaa to print this
}

}


}
Hi, try something like :

char s=word[i][0];
bool same(true);

for(int j(0);j<word[i].size();++j){
if(word[i][j]!=s) same=false;
}



//after that, same should be set to true with "aaaa" and false with "aaabaa"
#include <iostream>
#include<vector>
#include<string>

using namespace std;

void func(vector<string>);

int main()
{
vector<string> tester={"loop", "aaaaa"} ;

func(tester);

return 0;
}


void func(vector<string> word)
{

int count;



for(int i= 0; i<word.size(); ++i)
{
cout<<"\n ";
bool same(true);

for(int j=0; j<word[i].size(); ++j)
{
cout<<word[i][j] ;

char s=word[i][0];

for(int j(0);j<word[i].size();++j)
{
if(word[i][j]!=s) same=false;
}

}
if(same)
{
cout<<" "<<"all word same" ;
}

}

}



worked like a charm thanks
A better way to do it is to do that :
1
2
if(word[i]!=string(word[i].size(),word[i][0]) return false;
else return true;


The constructor string(size_t n, char c) fill a string of size n with the char c.
Last edited on
First, please use code tags when posting code.

Second, break problem into smaller pieces. It seems that the "vector" is irrelevant.

Are all characters of one word identical?
http://www.cplusplus.com/reference/algorithm/all_of/
1
2
3
4
5
6
7
8
9
bool homogenous( const std::string& word )
{
  if ( word.empty() ) return false; // or true, up to you

  auto it = word.begin();
  char token = *it;
  ++it;
  return std::all_of( it, word.end(), [token](char c){return c==token;} );
}


Simple pieces can then be used with many words.
1
2
3
4
vector<string> text;
for ( auto w : text ) {
  if ( homogenous(w) ) std::cout << w << '\n';
}
Hi Keskiverto,

can you show how to put the code in a tab, idk,

thanks
Use this
[.code]
your code
[./code]

Without dot inside beacon.

 
your code
Zaap, your comparing characters in the same word. That tell you if two words contain the same characters.

The tricky part to this problem is that you have to account for the number of occurences of the letters, not just whether the letters from one word appear in the other. A really simple way to do this is to sort the words and then compare them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

bool sameChars(string word1, string word2)
{
    sort(word1.begin(), word1.end());
    sort(word2.begin(), word2.end());
    return (word1 == word2);
}

int main()
{
    string str1, str2;

    while (cin >> str1 >> str2) {
	cout << str1
	     << (sameChars(str1, str2) ? " does " : " does not ")
	     << "contain the same characters as " << str2 << '\n';
    }
}


That runs in O(n*log(n)) time where n in the size of a word. A theoretically faster way is to create an array that counts the letters of each word and then compare the arrays. That runs in O(n) time, but words in human language are small and I suspect that the first method will outperform the second in practical applications.
Topic archived. No new replies allowed.