查看: 1575|回复: 10
|
课业问题!求教关于string compare
[复制链接]
|
|
问题:我先set 一个 char array,char word[]=" rogrammingconcept"然后要求user input 一个 string。。然后和我先前set好的char array compare对看是否相同。
假如我enter “gramming”,过后用 for loop来loop我的compare,里面有if else做checking,就会一直loop到 char word[3]的g,if else statement 就check 到对字。。然后我的loop却会auto从新loop过。。。这样我根本无法check全部我enter字体!有什么coding是个以当 if else check到对的时候。。还会继续check下一个字!
拜托各位帮帮小弟忙!万分感激 |
|
|
|
|
|
|
|
发表于 20-7-2012 10:41 AM
|
显示全部楼层
假如我enter “gramming”,过后用 for loop来loop我的compare,里面有if else做checking,就会一直loop到 char word[3]的g,if else statement 就check 到对字。。然后我的loop却会auto从新loop过。。。这样我根本无法check全部我enter字体!有什么coding是个以当 if else check到对的时候。。还会继续check下一个字!
b8b8b8b8 发表于 20-7-2012 12:17 AM 
不明白你的问题…… =.=" |
|
|
|
|
|
|
|
发表于 20-7-2012 12:56 PM
|
显示全部楼层
大概看得懂,你要的是contain/find 不是compare,没必要用char一个个查那么麻烦啦。
不懂你是要什么语言。
c++: find
c#, java: contains
c: strstr |
|
|
|
|
|
|
|
发表于 20-7-2012 04:19 PM
|
显示全部楼层
试下用if else statement 来 compare 你的 array 。 如果true 的话,compare 你那个word[xx] array , 再用loop++ 你的 xx |
|
|
|
|
|
|
|

楼主 |
发表于 20-7-2012 07:24 PM
|
显示全部楼层
回复 3# littlepenguin
我的是普通c++。我拍的题目照片不是很明。我用写好了!题目是user要更新password。题目已经set好两个array.
1) char oldPass[20]="My~Password";
1) char special[]="~!@#$%^&*_-+|";
更新password有几个条件
1)新password里必须存在最少一个set好的special character
2)新password不可以有3个连续的字体跟oldPass一样
例如
我enter MyPass#001,答案是错!因为enter新的password "MyPass"里Pass是跟oldPass里的Pass一样。
enter My~Pwd#001,错!因为MY~P是已近超过3个连续字体是跟oldPass里的字体一样。
老师给的提示是用string length 和 string compare。
sem2老师刚教string compare之类东西我没专心听,结果来到sem4 array出string compare就去荷兰.gif) |
|
|
|
|
|
|
|
发表于 21-7-2012 12:55 PM
|
显示全部楼层
本帖最后由 andy5627 于 21-7-2012 12:57 PM 编辑
b8b8b8b8 发表于 20-7-2012 07:24 PM 
回复 3# littlepenguin
我的logic :
首先是要loop你的input password,
然后nested loop old password 和 special characters 来做checking。
用来做checking condition, 你需要declare一个boolean和一个counter来,打个比方:
比如:
boolean foundSpecial = false;
int similiar = 0;- for(int i=0; i<20; i++) { // let say你的char限于20个
- if(similiar < 3) { // 用于查是不是跟old password有一样的
- for(int j=0; j<20; j ++){
- if(strcmp( newPass[i], oldPass[j]) == 0){
- similar++;
- break;
- }
- else{
- similiar = 0;
- }
- }
- }
-
- if( !foundSpeical ) {
- for(int k=0; k<13; k++) { // loop special characters
- if(strcmp( newPass[i], special[k] ) == 0 ){
- foundSpecial = true;
- }
- }
- }
- }
复制代码 大概是这样,没有试过跑,不知道有没有bug,给你作为参考。 |
|
|
|
|
|
|
|
发表于 21-7-2012 08:14 PM
|
显示全部楼层
本帖最后由 littlepenguin 于 21-7-2012 08:17 PM 编辑
b8b8b8b8 发表于 20-7-2012 07:24 PM 
回复 3# littlepenguin
如果要全部用char的话,用原始的方法咯。因为我习惯用string,char很少用。- const char oldPass[20]="My~Password";
- const char newPass[]="MyPass#001";
- const char special[]="~!@#$%^&*_-+|";
- bool fail = false;
- int length = sizeof newPass;
- for(int i = 0; i<length; i++)
- {
- for(int j = 0; j< 13; j++)
- {
- if(newPass[i] == special[j])
- {
- fail = true;
- break;
- }
- }
- }
- if(!fail)
- {
- length -= 2;
- for(int i = 0; i<length; i++)
- {
- for(int j = 0; j <18; j++)
- {
- if(newPass[i] == oldPass[j] && newPass[i+1] == oldPass[j+1]&& newPass[i+2] == oldPass[j+2])
- {
- fail= true;
- break;
- }
- }
- }
- }
- if(fail)
- cout<<"Invalid";
- else
- cout<<"Valid";
复制代码 |
|
|
|
|
|
|
|
发表于 21-7-2012 08:21 PM
|
显示全部楼层
andy5627 发表于 21-7-2012 12:55 PM 
我的logic :
首先是要loop你的input password,
然后nested loop old password 和 special characters ...
因为楼主要求的是3个char连在一起, 不是分开的。
|
|
|
|
|
|
|
|

楼主 |
发表于 22-7-2012 11:26 AM
|
显示全部楼层
littlepenguin 发表于 21-7-2012 08:14 PM 
如果要全部用char的话,用原始的方法咯。因为我习惯用string,char很少用。
谢谢你!我回家后尝试看。。有问题再请教您.gif)
|
|
|
|
|
|
|
|
发表于 30-8-2012 11:42 AM
|
显示全部楼层
类似这样,改一改
#include<iostream>
#include<string>
using namespace std;
void test(char* abc, char* def,int i)
{
for(int t = 0;t<i;t++)
{
if (*(abc+t) != (*(def+t)))
cout<<"NO"<<endl;
else
cout<<"Yes"<<endl;
}
}
int main()
{
char z[] = "APPLE";
char x[256], *px, *pz;
pz = z;
px = x;
cout<<"Enter a string";
cin>>x;
test(px,pz,5);
} |
|
|
|
|
|
|
|
发表于 8-9-2012 11:46 AM
|
显示全部楼层
因该是用string index,如果没错。java的 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|