answersLogoWhite

0


Best Answer

#include<iostream>

#include<string>

int compare (const std::string& a, const std::string& b)

{

int n=1;

std::string::const_iterator ai=a.begin(), bi=b.begin();

for (int n=1; ai!=a.end() && bi!=b.end(); ++ai, ++bi, ++n)

{

char ca = *ai;

char cb = *bi;

if (ca<cb) return n * (-1);

if (cb<ca) return n;

}

if (ai==a.end() && bi==b.end()) return 0;

if (ai==a.end()) return n * (-1);

return n;

}

int main()

{

std::cout

<< "When comparing strings, zero indicates the two strings are equal.\n"

<< "A negative value indicates the first string is less than the second string.\n"

<< "A positive value indicates the first string is greater than the second string.\n"

<< "The absolute value indicates the characters that differ in the strings. Thus\n"

<< "-10 or 10 indicate that the 10th characters show a difference.\n"

<< std::endl;

std::string x = "This is a string.";

std::string y = "This is another string.";

std::string z = "This is another.";

std::cout << "String x = "" << x << """ << std::endl;

std::cout << "String y = "" << y << """ << std::endl;

std::cout << "String z = "" << z << """ << std::endl;

std::cout << std::endl;

std::cout << "compare(x,x) = " << compare(x,x) << std::endl;

std::cout << "compare(x,y) = " << compare(x,y) << std::endl;

std::cout << "compare(x,z) = " << compare(x,z) << std::endl;

std::cout << "compare(y,x) = " << compare(y,x) << std::endl;

std::cout << "compare(y,y) = " << compare(y,y) << std::endl;

std::cout << "compare(y,z) = " << compare(y,z) << std::endl;

std::cout << "compare(z,x) = " << compare(z,x) << std::endl;

std::cout << "compare(z,y) = " << compare(z,y) << std::endl;

std::cout << "compare(z,z) = " << compare(z,z) << std::endl;

std::cout << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

To keep things as simple as possible, the following program uses strings rather than text files, however the algorithm is exactly the same for both. A text file is really nothing more than a disk-based array of characters and a string is also an array of characters so both can be treated in exactly the same way. The only real difference is that instead of indexing memory offsets, you index file offsets.

Note that the main trick with all search and replace algorithms is to leave the input unchanged and simply build up an output file, replacing any matching strings as you go. If you want to change the input file itself, once the output file is created simply close both files, swap their file names (rename them so the output file has the input file name and vice versa), then reopen the input file (which was the output file). You can then delete the output file (which was the original input).

#include<iostream>

#include<string>

using namespace std;

string search_replace (const string& input, const string& search, const string& replace)

{

// If the search string is empty, there's nothing to replace

// so return the input.

if (search.size()==0)

return input;

// Initialise the output.

string output = "";

// Initialise the compare positions.

unsigned input_position = 0;

unsigned search_position = 0;

// Scan through the input one character at a time

// until there are fewer characters remaining than

// there are in the search.

while (input_position < input.size() - search.size() + 1)

{

// Compare characters. If there is no match in the current

// positions, append the input character to the output and

// advance to the next character.

if (input[input_position] != search[0])

{

output += input[input_position];

++input_position;

}

else

{

// Keep track of where we start and end in the input.

unsigned start_position = input_position;

unsigned end_position = start_position;

// Keep track of the search position.

unsigned search_position = 0;

do

{

// Advance to the next character in the input and search.

++input_position;

++search_position;

// If the current input character is the same as the first

// search character and we haven't already found a second

// instance of this character, update the end position.

if ((end_position search.size())

{

// If we reached the end of the search string, append

// the replace string to the output.

output += replace;

}

else if (start_position != end_position)

{

// If we didn't find the entire search string but did

// find a second occurance of its first character,

// append everything excluding this character and

// set the input position to that character.

output += input.substr (start_position, end_position - start_position);

input_position = end_position;

}

else

{

// The search string was not found and we didn't find

// another occurance of the first search character so

// append everything up to and including the first

// unmatched character.

output += input.substr (start_position, input_position - start_position + 1);

++input_position;

}

}

}

// Append any and all remaining characters.

while (input_position < input.size())

{

output += input[input_position++];

}

return output;

}

int main()

{

string input = "Time looks like an arrow but fruit flies like a banana";

string search = "look";

string replace = "flie";

cout << "Input:\t\t"" << input << ""\n";

cout << "Search:\t\t"" << search << ""\n";

cout << "Replace:\t"" << replace << ""\n";

string output = search_replace (input, search, replace);

cout << "Output:\t\t"" << output << ""\n";

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<cassert>

namespace nonstd

{

class string

{

char* buffer {nullptr}; // inline assignment (C++11)

// friend operators (requiring access to private representation)

friend std::ostream& operator<< (std::ostream&, const string&);

friend bool operator== (const string&, const string&);

friend bool operator< (const string&, const string&);

friend bool operator<= (const string&, const string&);

friend bool operator!= (const string&, const string&);

friend bool operator> (const string&, const string&);

friend bool operator>= (const string&, const string&);

public:

~string ();

string ();

string (const char*);

string (const string&);

string (string&&);

string& operator= (const char*);

string& operator= (const string&);

string& operator= (string&&);

size_t size() const;

void clear ();

}; // class string

// output stream insertion operator overload

std::ostream& operator<< (std::ostream& os, const string& s)

{

// Put double quotes around string.

return os << '"' << s.buffer << '"';

}

// equality operator overload

bool operator== (const string& lhs, const string& rhs)

{

char* a=lhs.buffer;

char* b=rhs.buffer;

while (*a && *b && *a==*b) ++a, ++b;

return !*a && !*b;

}

// less-than operator overload

bool operator< (const string& lhs, const string& rhs)

{

char* a=lhs.buffer;

char* b=rhs.buffer;

while (*a && *b && *a==*b) ++a, ++b;

return (!*a && *b) (*a && *b && *a<*b);

}

// less than or equal to operator overload

bool operator<= (const string& lhs, const string& rhs)

{

char* a=lhs.buffer;

char* b=rhs.buffer;

while (*a && *b && *a==*b) ++a, ++b;

return (!*a && !*b) (!*a && *b) (*a && *b && *a<*b);

}

// not equal operator overload

bool operator!= (const string& lhs, const string& rhs)

{

char* a=lhs.buffer;

char* b=rhs.buffer;

while (*a && *b && *a==*b) ++a, ++b;

return *a != *b;

}

// greater-than operator overload

bool operator> (const string& lhs, const string& rhs)

{

char* a=lhs.buffer;

char* b=rhs.buffer;

while (*a && *b && *a==*b) ++a, ++b;

return (*a && !*b) (*a && *b && *a>*b);

}

// greater-than or equal to operator overload

bool operator>= (const string& lhs, const string& rhs)

{

char* a=lhs.buffer;

char* b=rhs.buffer;

while (*a && *b && *a==*b) ++a, ++b;

return (!*a && !*b) (*a && !*b) (*a && *b && *a>*b);

}

// destructor

string::~string ()

{

delete [] buffer;

}

// default constructor (constructs an empty string)

string::string (): buffer {new char [1]}

{

*buffer = 0;

}

// conversion constructor (constructs a nonstd::string from a C-style string)

string::string (const char* cstr)

{

size_t sz=1;

if (cstr)

{

char* p=const_cast<char*>(cstr);

while (*p++);

sz=(p-cstr);

}

buffer = new char [sz];

if (1<sz)

memcpy (buffer, cstr, sz);

else

*buffer=0;

}

// copy constructor

string::string (const string& str)

{

size_t sz=str.size()+1;

buffer = new char [sz];

memcpy (buffer, str.buffer, sz);

}

// move constructor

string::string (string&& str)

{

// take ownership of resource

buffer=str.buffer;

str.buffer=nullptr;

// leave str in a valid state

str.clear();

}

// conversion assignment operator overload (assigns a nonstd::string from a C-style string)

string& string::operator= (const char* cstr)

{

delete [] buffer;

size_t sz=1;

if (cstr)

{

char* p=const_cast<char*>(cstr);

while (*p++);

sz=p-cstr;

}

buffer = new char [sz];

if (1<sz)

memcpy (buffer, cstr, sz);

else

*buffer=0;

return *this;

}

// copy assignment operator overload

string& string::operator= (const string& str)

{

assert (this!=&str);

delete [] buffer;

size_t sz=str.size()+1;

buffer = new char [sz];

memcpy (buffer, str.buffer, sz);

return *this;

}

// move assignment operator overload

string& string::operator= (string&& str)

{

assert (this!=&str);

delete [] buffer;

// take ownership of str buffer

buffer=str.buffer;

str.buffer=nullptr;

str.clear(); // leave str in a valid state!

return *this;

}

void string::clear()

{

// make an empty string ("")

delete [] buffer;

buffer=new char [1];

*buffer=0; // null-terminator!

}

size_t string::size() const

{

// returns the length of the string (excludes null terminator)

char* p=buffer;

while (*p++);

return (p-buffer)-1;

}

}; // namespace nonstd

///////////////////////////

// Test driver functions //

///////////////////////////

void tester (const nonstd::string& str1, const nonstd::string& str2)

{

std::cout << "Comparing " << str1 << " with " << str2 << std::endl;

if (str1==str2) std::cout << str1 << " is equal to " << str2 << std::endl;

if (str1!=str2) std::cout << str1 << " is not equal to " << str2 << std::endl;

if (str1< str2) std::cout << str1 << " is less than " << str2 << std::endl;

if (str1<=str2) std::cout << str1 << " is less than or equal to " << str2 << std::endl;

if (str1> str2) std::cout << str1 << " is greater than " << str2 << std::endl;

if (str1>=str2) std::cout << str1 << " is greater than or equal to " << str2 << std::endl;

std::cout << std::endl;

}

int main()

{

nonstd::string a = "Hello world!";

nonstd::string b = "Hello everyone!";

tester (a, a);

tester (a, b);

tester (b, b);

tester (b, a);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus programme to compare strings?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Make programme for multification of three no in c plus plus?

result = a * b * c;


Does C allows you to use equality operators to compare strings?

Yes, it does.


What are the steps to execute a c plus plus programme?

Build it, link it, run it.


What is the use of strcmp function in c plus plus?

strcmp is used to compare two strings. If the return value is zero, the two strings are the same. If the return value is less than 0, then the first string is less than the second string, otherwise the first string is greater than the second string. Strings are compared lexicographically, character by character.


How will you compare turbo c to c plus plus?

"http://wiki.answers.com/Q/How_will_you_compare_turbo_c_to_c_plus_plus_language"


Do you have to use delete operator on C plus plus strings before exiting your application?

no you dont


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


How will you compare turbo c to c plus plus language?

"http://wiki.answers.com/Q/How_will_you_compare_turbo_c_to_c_plus_plus_language"


Can a user compile c programme using c plus plus compiler?

Usually, but not always. For example the following is legal in C, but illegal in C++: char new [3] = "ABC";


How can you get a specific string from large string using c plus plus strings?

std::string::substr();


How do you write a programme to print a plus bi in c plus plus?

#include &lt;iostream&gt; int main() { std::cout &lt;&lt; "a plus bi" &lt;&lt; std::endl; return 0; }


How do you use this function in c programme?

I don't use that function in C programme.