| | |
how to use strncmp function to compare two string input by user in C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 3
Reputation:
Solved Threads: 0
cpp Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> #include <iomanip> #include <cstring> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { char s1 = ""; char s2 = ""; cout << "Input String1: " ; cin >> *s1; cout << "Input String2: " ; cin >> *s2; if ( strcmp( s1, s2 ) > 0 ) cout << "\nThe first string is greater than the second string." << endl; else if ( strcmp( s1, s2 ) == 0 ) cout << "\nThe first string is equal to the second string." << endl; else if ( strcmp( s1, s2 ) < 0 ) cout << "\nThe first string is less than the second string." <<endl; return 0; }
Last edited by Tekmaven; Jun 4th, 2009 at 3:27 am. Reason: Code Tags
Your use of strcmp( ) is correct.
However, your strings aren't strings. You must allocate memory for s1 and s2 as arrays of char. Or declare them as char pointers and then allocate with the new operator.
Your subject says "strncmp( )". If you want to use that variant, which compares only up till a difference or that number of characters, whichever comes first, you must add a third argument, the number of characters limit.
However, your strings aren't strings. You must allocate memory for s1 and s2 as arrays of char. Or declare them as char pointers and then allocate with the new operator.
Your subject says "strncmp( )". If you want to use that variant, which compares only up till a difference or that number of characters, whichever comes first, you must add a third argument, the number of characters limit.
C++ Syntax (Toggle Plain Text)
if ( strcmp( s1, s2, 5 ) > 0 ) //5, for example
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
First, allocate sufficient memory for your strings. For example:
Now, what you have will work, to a point. The use of
I'll leave it as an exercise for the reader to look up the proper usage of that - it's been amply explained here on DaniWeb, and everywhere else.
C++ Syntax (Toggle Plain Text)
char s1[128] = ""; //exact size depends on how big an input you expect char s2[128] = "";
Now, what you have will work, to a point. The use of
cin >> s1; will limit your input to a single word. If you want to get in multi-word strings, you need to use the getline( ) method.I'll leave it as an exercise for the reader to look up the proper usage of that - it's been amply explained here on DaniWeb, and everywhere else.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: May 2009
Posts: 27
Reputation:
Solved Threads: 2
Dear you are declaring a char ( char s1 = "" ). Its not a string fis=rst declare a string as
char s1[10]; // string s1 of 10 characters
char s2[10]; //string s2 of 10 characters
Now take input as
cin.getline(s1,10,'\n');
cin.getlin(s2,10,'\n');
Now compare two strings. It will compile and work.
I hope you enjoy it.
char s1[10]; // string s1 of 10 characters
char s2[10]; //string s2 of 10 characters
Now take input as
cin.getline(s1,10,'\n');
cin.getlin(s2,10,'\n');
Now compare two strings. It will compile and work.
I hope you enjoy it.
•
•
•
•
Dear you are declaring a char ( char s1 = "" ). Its not a string fis=rst declare a string as
char s1[10]; // string s1 of 10 characters
char s2[10]; //string s2 of 10 characters
Now take input as
cin.getline(s1,10,'\n');
cin.getlin(s2,10,'\n');
Now compare two strings. It will compile and work.
I hope you enjoy it.
- Explicitly specifying the delimiter in
cin.getline(s1,10,'\n');isn't necessary. -
char s1[10]; // string s1 of 10 characters
You aren't taking the NULL-terminator '\0' into account. -
cin.getline(s1,10,'\n');: if you want this line to behave correctly then you should do one of the following:- or, you change it to:
cin.getline(s1,9,'\n'); - or you change
char s1[10];tochar s1[11];
- or, you change it to:
Last edited by tux4life; Jun 4th, 2009 at 3:13 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
To tux:http://www.cplusplus.com/reference/i...tream/getline/
"Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation"
EDIT Dave:Umm, Guess you were faster. Dani is loading real slow. I am thus using a console based browser. Sorry couldn't see you
"Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation"
EDIT Dave:Umm, Guess you were faster. Dani is loading real slow. I am thus using a console based browser. Sorry couldn't see you
Last edited by siddhant3s; Jun 4th, 2009 at 7:11 pm.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Well the objective is just to compare two strings right? Then even though strcmp can do the work wouldn't the usage as
Wouldn't the above implementation be more efficient? As we are using standard namespace.
>San you can go through this.
P.S: Guys the person "san gabriel" doesn't know about string classes I suppose may be new to c++ so wouldn't the new info be better?
c Syntax (Toggle Plain Text)
#include<string> using namespace std; //within main string str1 str2; //inputs to strings as cin<<string if(str1==str2)//comparision
>San you can go through this.
P.S: Guys the person "san gabriel" doesn't know about string classes I suppose may be new to c++ so wouldn't the new info be better?
Last edited by csurfer; Jun 4th, 2009 at 11:12 pm.
I Surf in "C"....
![]() |
Similar Threads
- How to compare string value (Java)
- how to reverse a numbers input by user (C)
- JAVA help (Java)
- Use string input for enum (C++)
- Masking a string input (Python)
- Can't Get Proper Input From User (Java)
- Compare String with Strings (Visual Basic 4 / 5 / 6)
- Lookup tables - how to perform a switch using a string (C++)
Other Threads in the C++ Forum
- Previous Thread: c++ boolean, array, and divx
- Next Thread: Problem deleting item from vector
Views: 597 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






