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. -
include<iostream>
-
using namespace std;
-
//#include<stdio.h>
-
#include<string.h>
-
int main()
-
{
-
char str1[10] = "XYZ";
-
char str2[10] = "XYZ";
-
char str3[10] ="BIRTHDAY";
-
-
cout<<"enter the first of two strings of your choice\n";
-
cin>>str1;
-
cout<<"enter the second string of your choice\n";
-
cin>>str2;
-
-
cout<<str1<<"\t";
-
cout<<"and"<<"\t";
-
cout<<str2<<"\t";
-
-
-
-
-
-
if(strncmp(str1,str2,20)==0)
-
-
{
-
cout<<"are equal\n";
-
}
-
else
-
{
-
cout<<"are Not equal\n";
-
}
-
return 0;
-
}
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
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?
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
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
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
I'm more than happy to help you.
Savage
thank you so much Savage i really appreciate your assistance while learning from you.
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
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: -
int strcmp(const char* s1, const char* s2)
-
{
-
while(1)
-
{
-
if (*s1 < *s2) return -1;
-
if(*s1 > *s2) return +1;
-
if(*s1 == '\0') return 0;
-
++s1;
-
++s2;
-
}
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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, ...)...
|
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.
|
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...
|
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...
|
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....
| |
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...
|
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...
|
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>&);
};
|
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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |