Joao brandao Posted February 21, 2012 at 08:55 PM Report Share #440609 Posted February 21, 2012 at 08:55 PM Boas pessoal, alguem me pode dizer como se usa a função strcountc ? Ate agora o que eu consigui foi isto: strcountc(s, ch); /* devolve o número de ocorrências do carácter ch na string s */ Cumprimentos, João Brandão Link to comment Share on other sites More sharing options...
KTachyon Posted February 21, 2012 at 09:05 PM Report Share #440611 Posted February 21, 2012 at 09:05 PM De certeza que não tens que ser tu a definir essa função? É que nas bibliotecas comuns essa função não existe. Mas em principio tens que dar os dois argumentos que estão indicados no comentário... “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare Link to comment Share on other sites More sharing options...
Joao brandao Posted February 21, 2012 at 09:18 PM Author Report Share #440615 Posted February 21, 2012 at 09:18 PM Eu tive a procura de mais informaçao e encontrei num post feito anteriormente e la o que tem é isto: int strcountc(char *str, char ch){ int i,noc=0; for(i=0;str!='\0';i++) if(str==ch) noc ++; return noc; } eu ja vi e percebo +/- o que ele tem aqui mas mesmo assim não compreendo como usar a função. O meu codigo ate agora estava assim: #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int soma; char texto[100],palavra[100]; cout<<"Digite o Texto"<<endl; cin.getline(texto,100); cout<<"Digite a palavra"<<endl; cin.getline(palavra,100); soma=strcountc(texto,palavra); // é so um teste isto mas mesmo assim esta mal cout<<soma<<endl; system("PAUSE"); return EXIT_SUCCESS; } Se alguem souber onde estou a errar ou como usar esta função eu agradecia 😛 Link to comment Share on other sites More sharing options...
KTachyon Posted February 21, 2012 at 10:19 PM Report Share #440625 Posted February 21, 2012 at 10:19 PM A função diz especificamente: int strcountc(char *str, char ch) Leva um ponteiro para char e um char. Tu estás a passar dois ponteiros para char para a função, é normal que não funcione. Para além disso tens que colocar a função antes do main. EDIT: E essa função está mal. Estás a comparar um ponteiro com um char e não termina, porque o ponteiro está sempre no mesmo sítio. int strcountc(char *str, char ch){ int i,noc=0; for(i=0;str!='\0';i++) // ciclo infinito if(str==ch) noc ++; // comparação ponteiro vs char return noc; } “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.” -- Tony Hoare Link to comment Share on other sites More sharing options...
Joao brandao Posted February 21, 2012 at 10:30 PM Author Report Share #440628 Posted February 21, 2012 at 10:30 PM hum kk 😛 obrigada pela ajuda Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now