Compare two strings input by the user.


#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <cstring>
#include <iomanip>
#include <cstdlib>

#include "stdafx.h"
#include <stdio.h>



int compare (void* ptr1, void*ptr2);

int _tmain (int argc, _TCHAR* argv[])
{
//local declarations
char a;
char*a=&a;
char b;
char*b=&b;
char lrg;
cout<<"Please enter two strings";
cin>>a;
cin>>b;

//statements
lrg=(*(int*) strcmp (&a, &b));
//char a[]<>char b[];
int strcmp=((a>b)? a: b);
for (int i=0; (a+i)!='\0'; i++)
cout<<"Larger string is:"<<compare(&a,&b);
return 0;
}

int compare (void* ptr1, void*ptr2)
{
if(*(int*)ptr1 >= *(int*)ptr2)
return 1;
else
return -1;
}

The errors I get are:
error C2040: 'a' : 'char *' differs in levels of indirection from 'char'
1>c:\users\jonathan\documents\visual studio 2010\projects\prog8-30\prog8-30\prog8-30.cpp(17): error C2040: 'b' : 'char *' differs in levels of indirection from 'char'
1>c:\users\jonathan\documents\visual studio 2010\projects\prog8-30\prog8-30\prog8-30.cpp(20): error C2088: '>>' : illegal for class
1>c:\users\jonathan\documents\visual studio 2010\projects\prog8-30\prog8-30\prog8-30.cpp(21): error C2088: '>>' : illegal for class
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

How do I resolve?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main()
{
    cout << "Enter two strings:";
    string s1, s2;
    cin >> s1 >> s2;

    if (s1.length() > s2.length())
    {
        cout << s1 << " is the larger string.";
    }
    else if (s1.length() < s2.length())
    {
        cout << s2 << " is the larger string.";
    }
    else
    {
        cout << "The strings have the same length.";
    }

    cin.get();

    return 0;
}
closed account (28poGNh0)
Why the complixity

supposed that z>y>.....>a>Z>Y>.....>A

I suggest this program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;

int main()
{
	char firstStr[100],secondStr[100];

	cout << "Please enter the first strings -> ";gets(firstStr);
	cout << "Please enter the second strings -> ";gets(secondStr);

	int i = strcmp(firstStr,secondStr);

	if(i==0)
	{
	    cout << "The two strings are equal" << endl;
	}else if(i>0)
	{
	    cout << "The first string is greather than the second string" << endl;
	}else cout << "The second string is greather than the first string" << endl;

	return 0;
}
closed account (28poGNh0)
@Josue Molina

what if I want my first string to be "hello word" and the second "this word"

you will notice that we cannot get the second string because you use cin >> statement maybe its better that you use getline function

also my program works only for smaller strings try not to enter more than 100 character
Last edited on
Why don't you just use a std::string??
closed account (28poGNh0)
I prefer using string .In fact I only work with strings .It just when It comes for beginers I write code using the old c string style because they begin with them first

my program's second version will be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# include <iostream>
using namespace std;

int main()
{
	string firstStr,secondStr;

	cout << "Please enter the first strings -> ";getline(cin,firstStr);
	cout << "Please enter the second strings -> ";getline(cin,secondStr);

	int i = firstStr.compare(secondStr);

	if(i==0)
	{
	    cout << "The two strings are equal" << endl;
	}else if(i>0)
	{
	    cout << "The first string is greather than the second string" << endl;
	}else cout << "The second string is greather than the first string" << endl;

	return 0;
}
I never used or use c-strings though I am a little more than a beginner.
Topic archived. No new replies allowed.