Help with strchr function
This is a discussion on Help with strchr function within the C++ Programming forums, part of the General Programming Boards category; Code:
char text[50];
char *searchPtr;
char character[26]={'
'};
int count=0;
int i,j;
cout<<"Enter strings: \n\n";
while(cin.getline(text,50)){
for(i=0;text[i]!='
'; i++){
text[i]=tolower(text[i]);
}
for(i=0;i<=25;i++){
...
-
Help with strchr function
Code:
char text[50];
char *searchPtr;
char character[26]={'\0'};
int count=0;
int i,j;
cout<<"Enter strings: \n\n";
while(cin.getline(text,50)){
for(i=0;text[i]!='\0'; i++){
text[i]=tolower(text[i]);
}
for(i=0;i<=25;i++){
for ( j = 0, count = 0; j <= 50; j++ ) {
searchPtr=&text[i];
while(searchPtr=strchr(searchPtr,'a'+i)){
++count;
searchPtr++;
}
}
character[i]=count;
}
}
cout<<endl<<endl;
for(i=0;i<=25;i++){
cout<<"("<<(char)('A'+i)<<" , "<<(char)('a'+i)<<") = "<<character[i]<<endl;
}
cout<<endl;
system("pause");
} can u help me to correct this? plz 
the counting of letters gives me wrong answer plz i need help . . .i really2x need it thanks advance
the output should be like this . . .
Enter strings:
Hello World
^Z
(H,h) = 1
(E,e) = 1
(L,l) = 3
(O,o) = 2
(W,w) =1
(R,r) =1
(D,d) =1
can u help how to get with this output?
if i input |hello world" the output should be like that it shouldn't be from
(A , a) to (Z,z) it only base on the letters that entered
-
and the Hat of Guessing
"= count" is not the same as "adding one".
EDIT: OIC, you're actually trying to count all the a, all the b, at one time. I guess that would work, although you appear to be starting at a weird place (shouldn't you always start at [0]?)
Also, printing might be odd as Salem points out below.
Last edited by tabstop; 08-22-2011 at 10:42 AM.
-
and the hat of mystery
So what answers ARE you getting?
I can help by reposting your code with some decent indentation.
Code:
int main()
{
char text[50];
char *searchPtr;
char character[26] = { '\0' };
int count = 0;
int i, j;
cout << "Enter strings: \n\n";
while (cin.getline(text, 50)) {
for (i = 0; text[i] != '\0'; i++) {
text[i] = tolower(text[i]);
}
for (i = 0; i <= 25; i++) {
for (j = 0, count = 0; j <= 50; j++) {
searchPtr = &text[i];
while (searchPtr = strchr(searchPtr, 'a' + i)) {
++count;
searchPtr++;
}
}
character[i] = count;
}
}
cout << endl << endl;
for (i = 0; i <= 25; i++) {
cout << "("
<< (char) ('A' + i)
<< " , "
<< (char) ('a' + i)
<< ") = "
<< character[i]
<< endl;
}
cout << endl;
system("pause");
} The only instantly odd thing I can see is using a char array to COUNT things.
It's not that you can't, it's just that they will be output as chars (and not numbers).
-
Don't PM me for help.
You need to sit down and rethink your logic.
1. Think about the problem and what it is you want to do
2. Come up with a solution to the problem
3. Write psuedocode on how you would solve the problem using paper and pencil
4. Convert your psuedocode to C
5. Run your program and see results
6. If you are still stuck, post your latest attempt, along with any error messages/warnings and output if you are getting something not expected.
Additionally, getline is delimited by the newline char in your example so your while statement doesn't really do anything for you.
As a side note, you shouldn't be using char arrays or cstring functions when you are working with C++. The C++ solution is at least 10 times easier to write.
Last edited by AndrewHunter; 08-22-2011 at 12:02 PM.
Reason: ...

Originally Posted by
anduril462
Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....

Originally Posted by
quzah
..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.
Popular pages Recent additions
Similar Threads
-
By everyone0 in forum C Programming
Replies: 12
Last Post: 11-16-2010, 01:35 PM
-
By everyone0 in forum C Programming
Replies: 1
Last Post: 11-16-2010, 01:10 PM
-
By sick in forum C Programming
Replies: 13
Last Post: 10-04-2008, 11:23 AM
-
By paperbox005 in forum C Programming
Replies: 8
Last Post: 08-08-2004, 02:29 AM
-
By client in forum C Programming
Replies: 2
Last Post: 05-12-2002, 12:41 PM