C++ :: Rules To Compare Two Strings Object
Sep 1, 2014
In my book I have the following example : Code:
std::string str = "Hello";
std::string phrase = "Hello world";
std::string slang = "Hiya"; and i have these two rules to compare 2 strings object :
if two strings have different lenghts and if every character in the shorter string is equal to the corresponding character of the longer string, than the shorter string is less than the longer string.
if any characters at corresponding positions of two strings differ, then the result of the string comparison is the result of comparing the first character at wich the strings differ then my book says : if we apply the rules of the comparison we know that phrase is greater than str( ok i've understood this ) and that slang is greater than both slang and phrase ( why ?)
explain me rule number two ? in phrase and slang the characters differ and the first character that differ is not H so why my book says slang is bigger than phrase ?
View 3 Replies
ADVERTISEMENT
Jan 4, 2014
I am trying to figure out how to go about comparing two strings of numbers. I have two files that both contain numbers 1-50, one file has multiple repeating numbers while the other one just has 1-50.
I want to compare the files and count how many of each number a occurred and make a chart with * next to the number. First I figured I would use the strings like an array and compare them using nested loops. Then I noticed I have single and double digit numbers. The numbers in the files are printed as:
1 44 5 34 4
2 22 7 55 4
...... etc
Compared too:
1
2
3
4
5
......
50
I thought about using string stream and converting the string to int but wouldn't it just be a huge number when set to the int variable? Then I thought about a array initialized with 1-50 and compared to the file but I still have the issue with single and double digit numbers.
My question is how can I just read one number at a time, either double or single digit?
View 11 Replies
View Related
Jun 13, 2013
I have two char* that have the same data in (hypothetically).
std::vector<char*> Buff;
Buff = Split(Line, '.');
char* A = "data", B;
B = Buff.at(0)
Where Split is a function that I made to split a string (Line in this case) into a char* vector, this string contains a line from a file. Line is char* too. The weird problem is when Buff data stored in its 0 position is given to B... because B is equal to A (hypothetically) but when this is compared to do certain functions they doesn't match!
Here an example:
std::vector<char*> Buff;
Buff = Split(Line, '.');
char* A = "map", B;
B = Buff.at(0) // Buff.at(0) should be "map" and is apparently "map"
[Code].....
NOTE: I didn't use switch to compare Cmd because I want it separately for easier debugging.
Is there something wrong with my codes?? or what happened here with those hex values before the string in my variables?
View 6 Replies
View Related
Oct 14, 2013
How would you compare two strings in an if statement to determine which comes first in alphabetical order?
Try and keep it simple because i am currently new to the language
View 2 Replies
View Related
Mar 16, 2014
i have been trying to compare a date format from SYSTEMTIME and a date from a text file(string).But its not working. I tried to change both to string(using osstringstream),char* and int(using sscanf) to do the comparison but with no luck. its pretty simple all i want to do is get the current system date and compare it with the date from the text file. Below is my code:
char szcurrentDate[MAX_PATH] = "";
char szdate_time[MAX_PATH];
SYSTEMTIME st;
GetLocalTime (&st);
GetDateFormat(LOCALE_USER_DEFAULT,NULL,&st,"yyyy-M-d ",szcurrentDate,MAX_PATH); //current system date
//std::ostringstream mm;
[code].....
note : i tried displaying just szcurrentDate and szdate_time they show the date exactly the same. in string,char* or int formats.
View 2 Replies
View Related
Oct 29, 2014
It should report whether or not, ignoring case, they are the same.
#include <stdio.h>
#include <string.h>
using namespace std;
[Code].....
View 1 Replies
View Related
Oct 29, 2014
This program is supposed to compare 2 strings and print out a 1 if the characters match and a 0 if they dont. It compiles but doesnt give me the correct output.
Code:
#include <stdio.h>
#include <string.h>
void func();
int main () {
func();
return 0;
[Code] ....
View 11 Replies
View Related
May 5, 2014
I am trying to compare a string that i have entered with a set of strings that have already been stored in a file. I am using strcmp function but i am not getting the result.
Code:
printf("
Enter string:");
scanf("%s",&m);
ptr_file =fopen("abc.text","r");
[Code] .....
View 10 Replies
View Related
Apr 2, 2013
I want to compare two string, and want to see differeince in int form. For example,
Code:
string first_string="0002AE1";
string second_string="0002AE2";
How can i calculate difference between two string? It is obvious difference between above two string is 1/-1, but difference would be 1.
View 2 Replies
View Related
Jul 10, 2013
Procedure to Compare 2 Strings with the following criteria
coding of the following function -
I have absolutely no clue where to start -
Given the following sets of numbers -
1154 1179 2154 2554 2484 2144 4515 1144 1517 4815 1481
Given the Index number of 1154
I want to search the numbers for the Index number of 1154
The search will return a True if I can find 3 or 4 same digits between the Index number and the 8 numbers
The search also have the following criteria -
meaning that -
1154 when compared to 1154 == true
1154 when compared to 1179 == false
1154 when compared to 2154 == true
1154 when compared to 2554 == false
1154 when compared to 2484 == false
1154 when compared to 2144 == false
1154 when compared to 4515 == true
1154 when compared to 1144 == true
1154 when compared to 1517 == true
1154 when compared to 4815 == true
1154 when compared to 1481 == true
the index number can also be of type - 1234, 1123, 1112, 1111
View 14 Replies
View Related
Apr 19, 2013
I have a list of objects that I need to read information from each object to compare to a user input prompt.
#include "Passenger.h"
#include "Reservation.h"
#include "Aircraft.h"
#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;
//Function Prototypes
void flightRoster(list<Reservation>&);
[Code] ....
View 1 Replies
View Related
Sep 3, 2014
Write a program to read strings from the standard input, concatenating what is read into a large concatenated string next ( ok I wrote this program and it works properly ), change the program to separate adjacent input strings by a space ( problem with the second part ). In my book i learnt the difference between the string input operator and the getline function, size member function, empty , but i can't undersand how to read two adjacent strings and how to separate them.....
View 3 Replies
View Related
Nov 13, 2014
Where are the c programming variable name rules defined? I usually use these two websites for figuring out these kind of things but I don't see it anywhere.
[URL]
View 4 Replies
View Related
Mar 27, 2014
I have been given an assignment that has to do with permutations. I am suppose to read a text file that contains the permutation rules and the text to be "permutated", and then output the rules and the new text into an output file.
So far, I've gotten this:
Code:
# include <stdio.h>
void printArray(FILE* file, char* array, int maxSize)
{
int i;
for (i = 0; i<maxSize; i++)
fprintf(file, "%c", *(array + i));
[Code] ....
Here is what the input file looks like:
Code:
0 1 2 3 4 5 6 7 8 9
4 5 6 0 9 7 8 1 2 3
Moderation in temper, is always a virtue; but moderation in principle, is a species of vice.
Here is what the output file is suppose to look like:
Code:
0 1 2 3 4 5 6 7 8 9
4 5 6 0 9 7 8 1 2 3
ratMnioodetem rpein al,ywa isvirsetu a t m;eod
buon r inaticipp,lerina s cpeis of icvies - Temho.
-ainaPes
The first two lines are the permutation rules. Currently I have figured out how to read the file into an array and then print it back out into a text.
What I want to do is figure out how to read only the first two line of the input file and store that as a permutation rules, and then continue reading the rest of the input file and store that separately as the text to be "permutated". And then eventually figure out how to apply the permutation to the text.
View 3 Replies
View Related
Jun 19, 2014
I've come across an interesting question involving a fixed-point arithmetic class.
Suppose I've got a template that implements a fixed-point quantity. It has two characteristics: width and number of fraction bits:
Code:
template <typename RAWTYPE, unsigned int FRACBITS>
class Fixed
{
....
};
So if I want a 32-bit value with 12 bits of fraction I can declare:
Code:
Fixed<int32_t, 12> x;
Suppose I've implemented a freestanding operator+() for fixed values, and I want to be able to add different types together:
Code:
template <typename RAWTYPE1, unsigned int FRACBITS1, typename RAWTYPE2, unsigned int FRACBITS2>
???? operator+(Fixed<RAWTYPE1, FRACBITS1> lhs, Fixed<RAWTYPE2, FRACBITS2> rhs)
{
???
}
What is the result type? Obviously, it's up to me to decide this. As reference, consider the type promotion rules for native types:
Code:
short a;
int b;
int result = a + b; In this case, the short value is promoted to the int value, and the addition happens on int.
It would seem a similar rule (go to the wider type) would be appropriate for fixed point. But there is another dimension to the problem, which is the number of fraction bits. Should you go to the wider type? Or the most precise type? Should you endeavor to minimize the number of bits which are discarded? What's the most intuitive rule?
View 10 Replies
View Related
Feb 27, 2014
I am new to C++ and am stuck on a program. I've got to create a password check program that makes sure the password rules are followed. Below is the code and the rules i have typed in the comments at the beginning of the program. I have to use loops and cant use arrays for this.
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
//Prompt user to entered a password to be tested
cout << "Password must be at least 8 characters long." << endl;
[Code] .....
View 2 Replies
View Related
Nov 13, 2014
I am having trouble with a program that queries the user for a noun and forms its plural on the basis of these rules:
a. If the noun ends in "y" remove the "y" and add "ies".
b. If the noun ends in "s", "ch", or "sh" add "es".
c. In all other cases, add "s".
I am having trouble getting started.
View 4 Replies
View Related
Mar 16, 2013
will copy constructor does object initialization using another already created object? I understand that it can be applied for object initialization and not for assignment.Is it correct?
View 10 Replies
View Related
Jul 3, 2014
I have a method to take a Tile object and make an instances of it based on some data from the original object. Than it is suppose to manipulate the a specific instance and save the results. The first loop through it works but it changes all instance as well as the base.
public static int recurse(int count, Tile[,] b,Huristic h,int check) {
if (check==1) {
boardState.Add(B)/>;
return check;
} if (check == 0)
[Code] .....
View 6 Replies
View Related
Dec 13, 2012
#include "B.h"
class A {
public :
A()
{
s_b = new B();
b = new B();
[Code] ....
In my project i have seen static object as above . But not able to know what is the exact use of it and how they are different from general object .
View 2 Replies
View Related
Apr 7, 2013
I am programming a translator, and I have it so that it detects words with spaces both in front of and behind them, so I did "string.append(space);" where space equals " ". That added a space to the end, but I still need a space added to the front.
View 1 Replies
View Related
Feb 12, 2014
I have a problem who must print the sentences who have lenght more than 20 characters. I dont know why, but it prints just the first words. Look what i made.
#include<stdio.h>
#include<conio.h>
int main()
[Code]....
For instance :
Give the number of sentences : 3
First sentence : I like the website bytes.com
Second sentence : I like more the website bytes.com
Third sentence : bytes.com
After I compile the program it should print the first two sentences.
View 2 Replies
View Related
Sep 11, 2014
a function returns a temporary object like
int myfun(){
int x = 0;
return x;
}
this function will return a temporary integer now void fun1(const int & num); this function can receive from myfun().BUT void fun2(int & num); this function cannot receive from myfun() Why is that, Moreover what is lifetime of a temporary object like one returned in myfun() ???
View 7 Replies
View Related
Mar 28, 2014
I am trying to use web api in order to return custom json response keys. For this i have return a custom class to return json response
Custom Class:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
[Code].....
View 2 Replies
View Related
May 4, 2013
I have a combobox with Items 1024
2048
4096
8192
String cach = form.comboCache.SelectedItem.ToString();
I am using the above code to retrive an item selected by user,But this line is giving an exception "Null Reference Exception, Object reference not set to an instance of an object"
View 1 Replies
View Related
Mar 28, 2014
I am using session to pass object from controller to custom class in MVC Web API. here is my code, i am getting error at session.
public HttpResponseMessage Get()
{
var person = db.Persons.ToList();
[Code]....
View 1 Replies
View Related