Guys all i want to know is how to do this, for example i have a string "experiment", in the word "experiment" we can make a word "ten", "mint", "rent", "time" and so on.I want to know how can i program it in C without repeating the letters of the word "experiment".. e=3 x=1 p=1 r=1 i=1 m=1 n=1 t=1.. means they cannot exceed on that numbers. I'm using this code in our project text twist. i can't figure out how to do this.. anyone please help me .. :( thanks guys.. hope you can help me..
Make a struct containing an array: struct lettercounts{int c[24];} Then count the letters. Then make a recursive function taking a lettercounts and calling itself multiple times.
Make a struct containing an array: struct lettercounts{int c[24];} Then count the letters. Then make a recursive function taking a lettercounts and calling itself multiple times.
Well, actually I misunderstood. You have a list of words which you want to check, right? Try making a lettercount of each one, and comparing them. You will want to write the following functions:
1 2 3
struct lettercount {int c[24];};
lettercount count_letters(string s);//counts the letters in the string
bool check_letters(lettercount source, lettercount query);//checks whether there are enough letters in source to make query.
is there other way to do this counting. but my friends uses strlen to compare each? is that correct because i
will use this code in text twist project eh. :( tnx for your help rocketboy9000
#include <iostream>
#include <string>
usingnamespace std;
struct lettercount {int c[24];};
lettercount count_letters(string s){
int i;
lettercount c;
for(i=0;i<24;i++)c.c[i];
/* Go through each character in s. If the char is a letter,
++ the count for that letter. */
for(i=0;i<s.length();i++){
if(s[i]>='A'&&s[i]<='Z')s[i]+='a'-'A';
if(s[i]>='a'&&s[i]<='z')c.c[s[i]-'a']++;
}
return c;
}
bool check_letters(lettercount s, lettercount q){
int i;
for(i=0;i<24;i++)if(s.c[i]<q.c[i])returnfalse;
returntrue;
}
int main(){
lettercount test=count_letters("experiment");
cout << check_letters(test,count_letters("mint")) << endl;//true
cout << check_letters(test,count_letters("yes")) << endl;//false
cout << check_letters(test,count_letters("time")) << endl;//true
}
The subtle difference is endl will put a newline and then flush the stream whereas \n will just be buffered and flush to stream later.
So if you have a lot of lines of code that has endl at the end, every line executed will flush the stream which can be very inefficient. The difference is only noticeable when you print say 1 million times the endl :P