|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I wrote a small program to find the occurrences of a character in an array of pointers to string. What is happening currently is that the program works for small case letters such as e w m but when i give uppercase letters E W it does not work and returns 0 as the number of times the char occured. Any insights would be helpful.
Code:
#include <stdio.h>
#include <string.h>
int main(){
int i,j=0, count = 0,itr = 0;
char temp[BUFSIZ];
char *itrc;
char *s[]= {
"We will teach you how to...",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"...all through C!"
};
for(i = 0;i<6;i++){
itrc = s[i];
printf("\n");
printf("\n\r%d", itrc);
while(itrc[j] != '\0'){
printf("\n\r%c", itrc[j]);
j++;
if(itrc[j] == 'e'){ // works for e but not for E
++count;
printf("\n\r%d", count);
}
}
j = 0;
//printf("\n\r%c", itrc[27]);
//printf("%d", s[i]);
}
printf("\n\r%d", count);
return 0;
}
|
|
#2
|
||||
|
||||
|
'E' is not the same as 'e'. Make two comparisons or use "toupper()" or "tolower()".
Secondly, don't be so dam' inconsiderate. Put your code in tags that preserve the formatting. See the "New users - HOW TO POST A QUESTION - READ THIS FIRST" thread (weird name for a thread, huh?) and the other forum FAQs.
__________________
Write no code whose complexity leaves you wondering what the hell you did. Politically Incorrect DaWei on Pointers Grumpy on Exceptions |
|
#3
|
||||
|
||||
|
What beats me is how you can write something as insightful as "// works for e but not for E" and yet the light still not dawn!
That very statement essentially answers your question.The ASCII code for 'e' is 101, and for 'E' it is 65. So why are you surprised? You are comparing a variable to 101 and asking why the result is not true when the variable contains 65. In the end it is all just numbers and 65 != 101. Only for humans and then only in some contexts might you consider 'e' as the same as 'E'. The circumstances where even that is so are subtle and ambiguous - computers do not do subtlety and ambiguity unless they are programmed to do so or have teh ambiguity removed by rules. Computers are not humans. If you want your code to behave as a human might expect, you have to code that behaviour. Code:
if( itrc[j] == 'e' || itrc[j] == 'E' ) Code:
if( tolower(itrc[j]) ) |
|
#4
|
|||
|
|||
|
Quote:
As an aside .... One of these days you need to do some research on microarchitecture versus instruction set architecture for a microprocessor. Components a few nanometres in size - those millions or billions of transistors that are fundamental to the working of a processor - are each subject to various physical perturbations with associated statistical distributions. And there are random physical events (eg stray alpha rays, neutrons from solder material) that can disturb the behaviour of processor and memory. One of the lurking reliability problems for software engineers is that they fundamentally assume the hardware behaviour will remain predictable as transistor densities go up. But electronic engineers are pushing physical limits in their hardware design and, at some point, there will be an impact on software reliability. Yes, it is true microprocessors are more reliable than the software which runs on them. But that's an indictment of software reliability, not a sign that hardware is inherently reliable.
__________________
Right 98% of the time, and don't care about the other 3%. |
|
#5
|
|||
|
|||
|
Code:
if( itrc[j] == 'e' || itrc[j] == 'E' ) I fully agree what you said above and am sorry for not inserting the code in the tags. I will keep that in mind. But i suppose you misunderstood my question. I know 'e' and 'E' are two different things in that their ascii values are different. But in my program iam not testing both the values. So initially i test for 'e' only and get 8 as the answer that is correct (no of times it has occured in the string) but when i make the comparison for 'E' usign the statement if( itrc[j] == 'E' ) i get 0 as the answer which is wrong. |
|
#6
|
||||
|
||||
|
Quote:
Then please edit your original post and put the code inside a code tags.
__________________
My worst nightmare was a pointless infinite loop. Work in progress; don't poke the curmudgeon! http://www.odonahue.com/ |
|
#7
|
||||
|
||||
|
Your program doesn't work for 'e' either. You just don't know it because your test data is limited. Take a look at where you are incrementing 'j'. Your never even looking at the first character of any string and you are running off the end of the string as well. Only that saves you there is the terminating nul character.
|
|
#8
|
||||
|
||||
|
Just so you know there are already existent library functions to search for occurrences of a char in a string, you can read about them here:
cstring It's a shame to waste your time on reinventing the wheel: |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Weird error... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|