c使用我自己的版本strcmp [英] c strings using my own version strcmp
问题描述
我无法弄清楚为什么我的strcmp根本不会cout,任何建议或帮助都表示赞赏!
I cannot figure out why my strcmp won't cout at all, any advice or help is appreciated!
#include< iostream>
#include< cstring>
#include< ctype.h>
#include< stdio.h> < br $>
int my_strcmp(char *,char *);
$
char * my_strcpy(char *,char *);
using namespace std;
int main(int argc,char * argv []){
char * str1 = argv [1];
$
char * str2 = argv [2];
$
my_strcmp(str1, str2);
$
cout<< str1<< endl;
cout<< str2<< endl;
$
my_strcpy(str1,str2);
cout<< str1<< endl;
cout<< str2<< endl;
$
返回0;
}
int my_strcmp(char * str1,char * str2 ){
char k;
char j;
char s;
  ;(str1 [i]){
k = str1 [i];
putchar(tolower(k));
i ++;
}
while(str2 [j]){
j = str2 [j];
putchar(tolower(s));
j ++;
}
cout<< str1<< "和" << str2<< endl;
$
if(strcmp(str1,str2)== 0){
返回0;
} // str1和str2相当于
if(strcmp(str1, str2)> 0){
返回-1;
} // str1以字母顺序排列在str2之前
if(strcmp(str1,str2)< 0){
返回1;
} // str1出现之后str2
}
char * my_strcpy(char * str1,char * str2){
int i;
for(i = 0; i< strlen(str2); i ++){
str1 [i] = str2 [i];
str1 [i] ='\ 0';
// cout<< str1 [i]<<结束;
返回str1;
}
}
#include<iostream>
#include<cstring>
#include<ctype.h>
#include<stdio.h>
int my_strcmp(char*, char*);
char* my_strcpy(char*, char*);
using namespace std;
int main(int argc, char* argv[]) {
char* str1 = argv[1];
char* str2 = argv[2];
my_strcmp(str1, str2);
cout << str1 << endl;
cout << str2 << endl;
my_strcpy(str1, str2);
cout << str1 << endl;
cout << str2 << endl;
return 0;
}
int my_strcmp(char* str1, char* str2) {
char k;
char j;
char s;
while(str1[i]) {
k = str1[i];
putchar (tolower(k));
i++;
}
while(str2[j]) {
j = str2[j];
putchar (tolower(s));
j++;
}
cout << str1 << " and " << str2 << endl;
if (strcmp(str1, str2) == 0) {
return 0;
} //str1 and str2 are equivalent
if (strcmp(str1, str2) > 0) {
return -1;
} //str1 appears before str2 in an alphabetical ordering
if (strcmp(str1, str2) < 0) {
return 1;
} //str1 appears after str2
}
char* my_strcpy(char* str1, char* str2) {
int i;
for(i = 0; i < strlen(str2); i++) {
str1[i] = str2[i];
str1[i] = '\0';
//cout << str1[i] << endl;
return str1;
}
}
推荐答案
在2/7/2019 11:52 PM,c strings写道:
On 2/7/2019 11:52 PM, c strings wrote:
int my_strcmp(char * str1,char * str2){
char k;
char j;
char s;
while(str1 [i]){
int my_strcmp(char* str1, char* str2) {
char k;
char j;
char s;
while(str1[i]) {
这不是您实际运行的代码。它使用变量`i`,但该名称未在任何地方声明。
This can't be the code you are actually running. It uses the variable `i`, but that name is not declared anywhere.
这篇关于c使用我自己的版本strcmp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!