473,464 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Create 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 1602
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*******@freenet.de]

Apr 8 '08 #2

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

Similar topics

16
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...
2
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...
4
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 -------------- /*...
16
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...
1
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
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...
4
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
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
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>...
8
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; };
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.