C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 3rd, 2009, 11:59 PM
insidemain(c) insidemain(c) is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 3 insidemain(c) User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 30 sec
Reputation Power: 0
Question Weird error...

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;
}

Reply With Quote
  #2  
Old July 4th, 2009, 12:27 AM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Crab
Click here for more information.
 
Join Date: Jun 2005
Posts: 5,691 sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 49th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 10 h 3 m 41 sec
Warnings Level: 5
Reputation Power: 4274
'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.
Comments on this post
jwdonahue agrees: 'e' != 'E'. Use code tags, etc.
__________________
Write no code whose complexity leaves you wondering what the hell you did.
Politically Incorrect DaWei on Pointers Grumpy on Exceptions

Reply With Quote
  #3  
Old July 4th, 2009, 03:57 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 3,712 clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level)clifford User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 23 h 23 m 3 sec
Reputation Power: 1165
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]) )

Reply With Quote
  #4  
Old July 4th, 2009, 04:55 AM
LittleGrin LittleGrin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 378 LittleGrin User rank is Captain (20000 - 30000 Reputation Level)LittleGrin User rank is Captain (20000 - 30000 Reputation Level)LittleGrin User rank is Captain (20000 - 30000 Reputation Level)LittleGrin User rank is Captain (20000 - 30000 Reputation Level)LittleGrin User rank is Captain (20000 - 30000 Reputation Level)LittleGrin User rank is Captain (20000 - 30000 Reputation Level)LittleGrin User rank is Captain (20000 - 30000 Reputation Level)LittleGrin User rank is Captain (20000 - 30000 Reputation Level)LittleGrin User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 3 Days 2 h 2 m 27 sec
Reputation Power: 259
Quote:
Originally Posted by clifford
computers do not do subtlety and ambiguity unless they are programmed to do so or have teh ambiguity removed by rules.

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

Reply With Quote
  #5  
Old July 4th, 2009, 09:12 AM
insidemain(c) insidemain(c) is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 3 insidemain(c) User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 30 sec
Reputation Power: 0
Question

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.

Reply With Quote
  #6  
Old July 4th, 2009, 09:24 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,381 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 5 h 12 m 51 sec
Reputation Power: 563
Quote:
Originally Posted by insidemain(c)
I fully agree what you said above and am sorry for not inserting the code in the tag


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/

Reply With Quote
  #7  
Old July 4th, 2009, 09:37 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Click here for more information.
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 2,381 jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level)jwdonahue User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 5 h 12 m 51 sec
Reputation Power: 563
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.

Reply With Quote
  #8  
Old July 4th, 2009, 01:22 PM
nebelung's Avatar
nebelung nebelung is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 157 nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level)nebelung User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 5 Days 9 h 12 m 8 sec
Reputation Power: 202
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:

c Code:
Original - c Code
  1. char targetChar = 'E';
  2.  
  3. for(size_t i = 0; i < sizeof(s)/sizeof(s[0]); ++i){
  4.     char* pch = strchr(s[i], targetChar );
  5.     while (pch != NULL){
  6.         ++count;
  7.         pch = strchr(pch+1, targetChar);
  8.     }
  9. }

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Weird error...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
Stay green...Green IT