C STRING(比较) [英] C STRING (Comparison)

查看:112
本文介绍了C STRING(比较)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello亲爱的研究员.. !!

我为两个字符串的比较编写了一个代码,但问题实际上是它的OUTPUT,从用户那里得到字符串后,程序刚刚完成没有输出......

请注意......我正在等待您的回复。 !!!!



strcmp函数的正确输出是什么.. !!另外我怎么写这个请给我一个例子来说明至少我试一试.... P L E A S. !!



Hello Dear Fellows .. !!
I write a code for the comparison of two character strings, but the problem really is its OUTPUT, after getting the string from user the program just finished having no output ...
please have an eye .. I''m keenly waiting your response . !!!!

What is the correct output of the function strcmp.. !! Also how can i write this please give me an example to illustrate at least i give a try.... P L E A S E . !!

#include "stdafx.h"
#include <iostream>
#include <cstring>

using namespace std;

int strcmp(char str[], char str2[])
{
		if (str < str2)
		
			return -1;
		/*{
			cout << "l e s s e r" << endl;
		}*/
		else if (str > str2)
		
			return 1;
		/*{
			cout << "g r e a t e r" << endl;
		}*/
		else if (str == str2)
		
			return 0;
		/*{
			cout << "e q u a l i t y" << endl;
		}*/
                      // what should i write here.. ?? this is causing a warning message from compiler.

}

int main()
{ 
	const int length = 15;
	char red[length], black[length]; // two strings that can hold 15 characters including null character.
	cout << "Enter First STRING: ";
	cin.getline(red, length+1); //  user allowed to enter a string of 15 characters + 1 for null character. 
	cout << "Enter Second STRING: ";
	cin.getline(black, length+1);
	strcmp(red, black);
	system("Pause");
	return 0;

}

推荐答案

除了你忘了输出strcmp函数的结果外,函数本身还有一个主要问题。它比较两个字符串的地址,而不是它们的内容。请注意,当您引用与引用第一个元素的指针相同的数组名称时。



strcmp应该包含一个比较字符的循环用字符表示两个字符串的内容。
Besides that you forgot to output the result of you strcmp function, the function itself has a major problem. It compares the addresses of the two strings, not their contents. Note that when you refer to the name of an array that is the same as referring to the pointer to the first element.

strcmp should instead contain a loop that compares character by character the contents of the two strings.


你应该使用std :: string而不是C风格的字符数组。它更容易使用,而且更不容易出错,包括std :: string的默认比较通常有用的事实。
You should use std::string instead of C-style characters array. It is much easier to use and far less error prone including the fact that default comparison of std::string does something that is usually helpful.


它没有输出任何东西,因为你'没有告诉它。您已经使用cout输出消息来提示用户输入,但在调用strcmp后,您只能调用system(pause);。



正如bigcdh所说,使用标准的strcmp函数,检查结果并输出相应的内容。



如果你打算自己实现strcmp,你可能不应该在其中输出任何内容,因为您可能想在其他情况下使用它。你注释掉输出语句的地方,如果你将它们取消注释,我会期望编译器错误。最后一个,在最后一个返回语句之后,我不知道C编译器是那么聪明,但它实际上是无法访问的代码 - 如果前两个如果'是假的,那么第三个将按照定义是的,所以返回后该函数中的任何内容都不可执行。
It''s not outputting anything because you''ve not told it to. You''ve used cout to output messages to prompt the user for input, but after calling strcmp, you only call "system("pause");".

As "bigcdh" has said, use the standard "strcmp" function, check the result and output something accordingly.

If you are going to implement strcmp yourself, you probably shouldn''t output anything in it because you might want to use it in other situations. The places where you have commented out outputting statements, if you uncommented them as-is I would expect compiler errors. The last one, after the last return statement, I didn''t know the C-compiler was that intelligent, but it is in fact unreachable code - if the first two if''s are false, then the third one will by definition be true, so nothing in that function after the return will be executable.


这篇关于C STRING(比较)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆