473,769 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R2 , section 5.4

PURPOSE: see comments.
OUTPUT: negative integer as output even for equal strings
/* A string comparison function from K&R2
* example code from section 5.4,page 106
*
* returns +ve number , if 1st string is greater than 2nd
* return -ve number, if 2nd string is greater than 1st
* returns 0, if both are equal
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum MAXSIZE { ARR_SIZE = 1000 };
int my_strcmp( char*, char* );

int main( )
{
char s1[ARR_SIZE], s2[ARR_SIZE];

strcpy( s1, "s" );
strcpy( s2, "s" );

printf( "%s\t%s\t --%d\n", s1, s2, my_strcmp( s1, s2 ));
return EXIT_SUCCESS;
}

int my_strcmp( char* s1, char* s2 )
{
for( ; *s1 == *s2; s1++, s2++ )
{
if( s1 == '\0' )
{
return 0;
}
}

return *s1 - *s2;
}

/*

if( *s1 *s2 )
{
return 1;
}
else if( *s1 < *s2 )
{
return -1;
}
else
{
return 0;
}
*/
=============== ===== OUTPUT =============== =======
Welcome to the Emacs shell

/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra section_5-5.c
/home/arnuld/programs/C $ ./a.out
s s ---151
/home/arnuld/programs/C $ ./a.out
s s ---66
/home/arnuld/programs/C $ ./a.out
s s ---122
/home/arnuld/programs/C $ ./a.out
s s ---193
/home/arnuld/programs/C $


-- http://lispmachine.wordpress.com/

Please remove capital 'V's when you reply to me via e-mail.

Apr 8 '08 #1
1 1623
arnuld schrieb:
PURPOSE: see comments.
OUTPUT: negative integer as output even for equal strings
/* A string comparison function from K&R2
* example code from section 5.4,page 106
*
* returns +ve number , if 1st string is greater than 2nd
* return -ve number, if 2nd string is greater than 1st
* returns 0, if both are equal
*
*/
[SNIP]
int my_strcmp( char* s1, char* s2 )
{
for( ; *s1 == *s2; s1++, s2++ )
{
if( s1 == '\0' )
Oops. I think you want:

if ( *s1 == '\0' )
{
return 0;
}
}

return *s1 - *s2;
}
--
Irrwahn Grausewitz [ir*******@freen et.de]

Apr 8 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1684
by: TTroy | last post by:
I FOUND MAJOR ERRORS in K&R2 (making it almost useless for the herein mentioned topics). K&R2 Section 5.9 Pointers vs. Multidimension Arrays starts of like this... "Newcomers to C are somtimes confused about the difference between a two-dimensional array and an array of pointers..." then continues to explain int *b; to be...
2
2296
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers yet. i have created a solution myself by modifying the example programme of section 1.19. i tried to find the source-code of K&R2 using Google. i found the home
4
1570
by: arnuld | last post by:
as i said, i have restarted the book because i overlooked some material. i want to have some comments/views on this solution. it runs fine, BTW. ------------------ PROGRAMME -------------- /* K&R2 section 1.5.3, exercise 1-8 write a programme to count blanks, tabs and newlines */
16
1809
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before looking at CLC-Wiki for K&R2 solutions: --------------- PROGRAMME ------------ /* K&R2 section 1.5.3, exercise 1-9 STATEMENT: write a programme to copy its input to output replacing
1
1391
by: arnuld | last post by:
this is "word counting" example programme from K&R2 section 1.5.4. it runs without any error/warning but does not work properly, i.e. the OUTPUT 1 should be: NEWLINES: 1 WORDS: 1 CHARs: 5
19
2407
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical
4
1470
by: arnuld | last post by:
/* K&R@, 1.5.1 - File Copying */ #include<stdio.h> int main() { int c; while((c = getchar())!= EOF) putchar(c);
6
2809
by: arnuld | last post by:
This is the example from section 4.2, page 71 of K&R2: double atof( char s ) { int i, sign; double val, power; for( i = 0; isspace( s ); ++i )
0
1821
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html> there are some ambiguous errata, for which I propose solutions. Any comments are welcome.
8
1587
by: arnuld | last post by:
This is the code form section 6.5 of K&R2: struct tnode { char *word; int count; struct tnode *left; struct tnode *right; };
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.