473,473 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

compare two string using stryncmp

91 New Member
i'm comparing two string using the function stryncmp,but my problem is when i execute the program it doesn't give me the correct values.actually it gives me same results as the function strycmp

this is the code i did.
Expand|Select|Wrap|Line Numbers
  1. include<iostream>
  2. using namespace std;
  3. //#include<stdio.h>
  4. #include<string.h>
  5. int main()
  6. {
  7. char str1[10] = "XYZ";
  8. char str2[10] = "XYZ";
  9. char str3[10] ="BIRTHDAY";
  10.  
  11. cout<<"enter the first of two strings of your choice\n";
  12. cin>>str1;
  13. cout<<"enter the second string of your choice\n";
  14. cin>>str2;
  15.  
  16. cout<<str1<<"\t";
  17. cout<<"and"<<"\t";
  18. cout<<str2<<"\t";
  19.  
  20.  
  21.  
  22.  
  23.  
  24. if(strncmp(str1,str2,20)==0)
  25.  
  26. {
  27. cout<<"are equal\n";
  28. }
  29. else
  30. {
  31. cout<<"are Not equal\n";
  32. }
  33. return 0;
  34. }
Sep 18 '07 #1
8 1954
Savage
1,764 Recognized Expert Top Contributor
That's because you have set count to 20.

If one of your strings has less than count then comparison will stop on the first reached string terminator.

Lower the count to 10 and it should work

Savage
Sep 18 '07 #2
Nkhosinathie
91 New Member
That's because you have set count to 20.

If one of your strings has less than count then comparison will stop on the first reached string terminator.

Lower the count to 10 and it should work

Savage
i've changed that 20 to 10,may i ask? is the function comparing the length of the string or it is comparint whether the strings are same names?
Sep 18 '07 #3
Savage
1,764 Recognized Expert Top Contributor
i've changed that 20 to 10,may i ask? is the function comparing the length of the string or it is comparint whether the strings are same names?
It compares both.If length of two strings is not the same there is no chance that string would be same, so it returns from the function.If lengths are same,then it compares it's values.

Savage
Sep 18 '07 #4
Nkhosinathie
91 New Member
It compares both.If length of two strings is not the same there is no chance that string would be same, so it returns from the function.If lengths are same,then it compares it's values.

Savage
thank you so much for the help,it means i've finish the first of the ten programs of my assignment. thank you
Sep 18 '07 #5
Savage
1,764 Recognized Expert Top Contributor
thank you so much for the help,it means i've finish the first of the ten programs of my assignment. thank you
I'm more than happy to help you.

Savage
Sep 18 '07 #6
Nkhosinathie
91 New Member
I'm more than happy to help you.

Savage
thank you so much Savage i really appreciate your assistance while learning from you.
Sep 18 '07 #7
Savage
1,764 Recognized Expert Top Contributor
thank you so much Savage i really appreciate your assistance while learning from you.
Thank you very much,Nkhosinathie :D

Savage
Sep 18 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
Not true:
It compares both.If length of two strings is not the same there is no chance that string would be same, so it returns from the function.If lengths are same,then it compares it's values.
It compares corresponding characters so long as they match and one of them is not a null terminator. This is a typical implementation:
Expand|Select|Wrap|Line Numbers
  1. int strcmp(const char* s1, const char* s2)
  2. {
  3.     while(1)
  4.     {
  5.          if (*s1 < *s2) return -1;
  6.          if(*s1 > *s2) return +1;
  7.          if(*s1 == '\0') return 0;
  8.          ++s1;
  9.          ++s2;
  10.     }
  11. }
  12.  
You do not need to know the lengths of the strings in order to compare them.

strncmp() just has a limit on the loop. If the loops runs the correct nunber of cycles, the function returns zero.
Sep 24 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Earl Purple | last post by:
On VC++.NET it is implemented like this static int __cdecl compare ( const _Elem *_First1, const _Elem *_First2, size_t _Count ) { // compare [_First1, _First1 + _Count) with [_First2, ...)...
9
by: geronimo_me | last post by:
Hi, I am atempting to compare part of a field with the whole of another field in access. Is this possible? Basically I have 2 tables with the following info: Table1 Field1 = MR.
7
by: unified | last post by:
Ok, I'm working on a program that is supposed to compare each letter of a string that is put into a stack and a queue. It is supposed to tell whether or not a word is a palindrome or not. (a...
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
0
by: William Stacey [MVP] | last post by:
Trying to figure out Dictionary<> and using CaseInsensitive Comparer<> like I did with normal Hashtable. The Hashtable can take a case insenstive Comparer and a Case insensitive HashCode provider....
6
by: Maileen | last post by:
Hi, I have the following code : Function GetRequestType(ByVal EvDt As String, ByVal StPeriod As String, ByVal EdPeriod As String, ByVal TaskType As String) As Integer Dim strtest As String Dim...
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
1
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
6
by: aznimah | last post by:
hi, i'm work on image comparison. i'm using the similarity measurement which i need to: 1) convert the image into the binary form since the algorithm that i've use works with binary data for the...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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: 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...
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.