Home > Back-end >  Want to ask next how to add a more length...
Want to ask next how to add a more length...

Time:09-22

#include
#include
# define MAX_NUM 100
Typedef struct STR {
Char ch [MAX_NUM + 1];
int length;

} * SString;
//string to initialize the
SString initSString ()
{
SString STR=(SString) malloc (sizeof (struct STR));
if(! STR)
Printf (" dynamic memory allocation failure! \n");
STR - & gt; Length=0;

//printf (" string of initial success! \n");
return str;

}
//to determine whether a string is empty
Int isEmpty (SString STR)
{
If (STR - & gt; Length==0)
{
return 1;
}
return 0;
}
//string assignment
SString strAssign (SString STR, char [] s)
{

int i=0;
[I] while (s)
{
STR - & gt; Ch [+ + STR - & gt; length] [I]=s;
i++;
}
return str;
}
//print string
Void printSString SString (STR)
{
int i;


for(i=1; i<=STR - & gt; length; I++)
Printf (" % c ", STR - & gt; Ch [I]);
printf("\n");
}
//string copy
SString strCopy (SString str1, SString str2)
{
int i;
for(i=1; i<=str2 - & gt; length; I++)
{
Str1 - & gt; Ch [I]=str2 - & gt; Ch [I];
}
Str1 - & gt; Length=str2 - & gt; length;
Return the str1;
}
SString strCat (SString str1, SString str2)
{
SString str3=initSString ();
int i;
for(i=1; i<=str1 - & gt; length; I++)
Str3 - & gt; Ch [I]=str1 - & gt; Ch [I];
for(i=1; i<=str2 - & gt; length; I++)
Str3 - & gt; Ch [I + str1 - & gt; length]=str2 - & gt; Ch [I];
Str3 - & gt; Length=str1 - & gt; Length + str2 - & gt; length;
Return str3;

}
//get the length of the string
Int getStrLength SString (STR)
{
Return the STR - & gt; length;
}

//in str1 pos position into the chain str2
SString strInsert (SString str1, int pos, SString str2)
{
If (pos<1 | | pos> Str1 - & gt; Length)
{
Printf (" insert position error! \n");
exit(0);
}
int i;
For (I=pos. i<=str1 - & gt; length; I++)
{
Str1 - & gt; Ch [I + str2 - & gt; length]=str1 - & gt; Ch [I];
}
for(i=1; i<=str2 - & gt; length; I++)
{
Str1 - & gt; Ch [I] + pos - 1=str2 - & gt; Ch [I];
}
Str1 - & gt; Length=str2 - & gt; Length + str1 - & gt; length;
Return the str1;
}

//delete specified location characters
SString strDelete (SString STR, int pos, int len)
{
If (pos<1 | | pos> STR - & gt; Length | | len + pos - 1 & gt; STR - & gt; Length)
{
Printf (" delete position error! \n");
exit(0);
}

int i;
For (I=pos + len; i<=STR - & gt; length; I++)
{

STR - & gt; Ch [I - len]=STR - & gt; Ch [I];
}
STR - & gt; Length=STR - & gt; Length - len;
return str;

}
//o substring
SString subString (SString STR, int pos, int len)
{
If (pos<1 | | pos> STR - & gt; Length | | len + pos - 1 & gt; STR - & gt; Length)
{
Printf (" input the wrong location! \n");
exit(0);
}
int i;
SString subStr=initSString ();
for(i=1; i<=len; I++)
{
SubStr - & gt; Ch [I]=STR - & gt; Ch [I + pos - 1);
}
SubStr - & gt; Length=len;
Return the subStr.
}
//destruction of string
Void destorySString (SString str1)
{

Free (str1);
}
Int main ()
{
SString STR=initSString (), str2=initSString ();
Char s [100], [100] s2, s3 [100], s4 [100].
Printf (" please enter a string str1: \ n ");
The scanf (" % s ", s);



STR=strAssign (STR, s);
Printf (" print strings str1: \ n ");
PrintSString (STR);

Printf (" copy strings str1 to string str2 \ n ");

StrCopy (str2, STR);
Printf (" print string str2: \ n ");

PrintSString (str2);

/* SString str3=strCat (STR, str2);
PrintSString (str3); */
Int insertLocation;

Printf (" please enter into position, insert string str4: \ n ");


The scanf (" % d ", & amp; InsertLocation);
The scanf (" % s ", s4);
SString str4=initSString ();
Str4=strAssign (str4, s4);


STR=strInsert (STR, insertLocation str4);
Printf (" print after insert the str4 strings str1: ");
PrintSString (STR);


Int delLocation delLength;
Printf (" please input strings str1 delete character location and length: \ n ");
The scanf (" % d % d ", & amp; DelLocation, & amp; DelLength);

StrDelete (STR, delLocation delLength);
PrintSString (STR);

Int subLocation subLength;
Printf (" please input strings str1 substring location and length: \ n ");
The scanf (" % d % d ", & amp; SubLocation, & amp; SubLength);
SString subStr=the subString (STR, subLocation subLength);
PrintSString subStr ();

Printf (" destruction of string st1: \ n ");
DestorySString (STR);


return 0;
}
Where is the last main function and a comparison of the length of the string

CodePudding user response:

Int strcmplen (char * s, char * a) {
Return strlen (s) & gt; Strlen (a);
}

CodePudding user response:

//comparing str1 and str2 length, if the length of the str1 not less than the length of str2, return true; Otherwise it returns false 
Bool compareSString (SString str1, SString str2)
{
If (getStrLength (str1) & gt;=getStrLength (str2))
{
return true;
}
The else
{
return false;
}
}

CodePudding user response:

For example
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related