strings in c

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..

-joenel
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.


i dont know how to do that function.. :(
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
uhm can you show me the full code please. then after you show me i'll try to figure it out line by line.. tnx rocketboy9000 :D
OK.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace 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])return false;
	return true;
}

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
}
thx rocketboy9000 :D..
i'll convert that in c.. beacuse i don't know c++..
btw what in endl in c?
endl is just a "\n".
endl is just a "\n".


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
closed account (z05DSL3A)
sohguanh,

\n will (should) flush the buffer with printf()
you guys are all genius tnx for the help :D.. sorry beacuse its my first time to take c++ this second sem.. we used to program in c
Topic archived. No new replies allowed.