好的,有两个问题。 首先
Q1。我在运行时分配了足够的内存,仍然连续两次运行案例6后,s1
打印垃圾,第三次运行案例6 s1
的值消失了
the first image shows the garbage value at the end of string
[第二张图片显示字符串1已消失] [2]
Q2。 STCPY( COPY FUCTION)
无效,显示segmentation fault
,但我又重新分配了足够的内存。(我需要将s2
复制到s1
,因此我将L2(string 2's length)
重新分配给s1
,以免造成内存浪费)
这里是代码:
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int STRLEN(char* S){
int i=0;
while(S[i] != '\0'){
i++;
}
return i;
}
int SUBSTR(char* str1, char* str2){
//string str1;
//string str2;
int L1=0,L2=0;
int i,j,flag=0;
int count=0;
/*cout<<"\nEnter main string greater characters(str1) : ";
cin>>str1;
cout<<"\nEnter phrase to find (str2) : ";
cin>>str2;
*/
L1 = STRLEN(str1); //calculate legth of strings
L2 = STRLEN(str2);
for(i=0;i<L1;i++){
count = 0;
flag = 0;
for(j=0;j<L2;j+=1){
if(str1[i]==str2[j])
{
//good
if(j==L2) //successful phrase's each character
traversed
{
break;
}
i++;
count++;
//j++;
}
else
{
break;
}
} //terminate inner for loops
{
i -= count; //i was incremented explicitly in
inner loop. for normal operation,
} // i has to be decremented by equal
no. of increments in inner loop
if(j==L2) //flag for successful traversing of phrase 2
in string 1
{
flag = 1;
break;
}
}
if(flag == 1)
{
cout<<"\nSUBTRING PRESENT AT "<<(i+1);
}
return 0;
}
int STREQL(char* str1, char* str2){
int flag = 0;
int L1=0,L2=0;
int i,j;
int count=0;
/* cout<<"\nEnter first word : ";
cin>>str1;
cout<<"\nEnter second word : ";
cin>>str2;*/
char* p1;
char* p2;
p1 = str1;
p2 = str2;
while(*(p1) != '\0')
{
if(*(p1) == *(p2))
{
//good
p1++;
p2++;
}
else{
flag = 1;
break;
}
}
if(flag == 1 )
{
cout<<"\n STRINGS ARE NOT EQUAL";
}
else{
cout<<"\n STRINGS ARE EQUAL";
}
return 0;
}
int STCPY(char* s1, char* s2){
int L2 = STRLEN(s2);
int L1 = STRLEN(s1);
cout<<"\nPASS 1";
char* s3 = (char*)realloc(s1,L2); //Note :- s3 and s1 point to
same memory location
cout<<"\nPASS 2";
while(s2 != '\0')
{
*s3 = *s2;
s3++;
s2++;
}
cout<<"\nPASS 3";
s3 -= L2;
s2 -= L2;
cout<<"\n STRING 1 = "<<s1;
cout<<"\n STRING 2 = "<<s2;
return 0;
}
int STRREV(){
return 0;
}
int STRLEN(char* str1, char* str2){
int L1=0,L2=0;
L1=STRLEN(str1);
L2=STRLEN(str2);
cout<<"\nSTRING 1 LENGTH = "<<L1;
cout<<"\nSTRING 2 LENGTH = "<<L2;
return 0;
}
int STRCAT(char* s1, char* s2)
{
int L1=0,L2=0;
int i,j;
L1=STRLEN(s1);
L2=STRLEN(s2);
//cout<<"\nSTRING 1 LENGTH = "<<L1;
//cout<<"\nSTRING 2 LENGTH = "<<L2;
char* s3 = (char*)realloc(s1,(L1+L2));
int cnt=0;
for(i=L1,s3 = s3+L1; *s2 != '\0'; i++)
{
*s3 = *s2;
s2++;
s3++;
cnt++;
}
s3 = s3-cnt-L1;
s2 = s2-cnt;
//cout<<"\nConcatenated string s3 = "<<s3;
//cout<<"\nConcatenated string s1 = "<<s1;
cout<<"\nSTRING 1 = "<<s1;
cout<<"\nSTRING 2 = "<<s2;
return 0;
}
int main(){
int i,j,choice;
char* s1;
char* s2;
cout<<"\nEnter string : ";
s1 = (char*)malloc(50);// ok i made this 50 bytes instead of 10
cin.getline(s1,50); // cause u guys arguing, but still that doesn't change the output
//cout<<s1;
cout<<"\nEnter string : ";
s2 = (char*)malloc(50);
cin.getline(s2,50);
//cout<<s2;
cout<<"\n------------MENU------------";
cout<<"\n1.SUBSTRING FIND";
cout<<"\n2.EQUAL CHECK";
cout<<"\n3.COPY STRING";
cout<<"\n4.REVERSE";
cout<<"\n5.STRING LENGTH";
cout<<"\n6.STRING CONCATENATION";
cout<<"\n7.EXIT";
cout<<"\n----------------------------";
do{
cout<<"\n\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
SUBSTR(s1, s2);
break;
case 2:
STREQL(s1, s2);
break;
case 3:
STCPY(s1, s2);
break;
case 4:
STRREV();
break;
case 5:
STRLEN(s1, s2);
break;
case 6:
STRCAT(s1, s2);
break;
case 7:
break;
}
}while(choice != 7);
return 0;
}
答案 0 :(得分:1)
更换&#34; PASS 2&#34;登录到:
while(*s2 != '\0')
{
*s3 = *s2;
s3++;
s2++;
}
观察s2!=&#39; \ 0&#39;基本上是指:s2!= nullptr,它永远不会是真的。这就是你出现分段错误的原因。
答案 1 :(得分:0)
C字符串以最终\ 0结束。所以内存占用是strlen(str)+ 1个字节。 你的STRCAT功能的问题在于你忘了为最终的\ 0添加空间(s3 = realloc(s1,L1 + L2 +1 ),你也忘了添加最终的\ 0( s3 [L1 + L2] = 0)。 您的STCPY函数的同义词,+ s2!= 0替换为* s2!= 0