Home > Back-end >  C string conversion for help
C string conversion for help

Time:05-20

For a string is
String a="123124125";
To turn him into an array [123124125] to do?

CodePudding user response:

The substring intercept string

CodePudding user response:

Reference:
 # include 
# include

using namespace std;

Int main ()
{
Int b [10]={0}, j=0, I=0;
String a="1234124 5125126127,12,1";
While (a [I]) {
If (a [I]! )=', '
B=b [j] [j] * 10 + a, [I] - '0'.
The else j++;
i++;
}
for(i=0; I<=j; I++)
Printf (" % d, b [I]);

return 0;
}

CodePudding user response:

Fyi:
 # include & lt; Stdio. H> 
Char [] s="123 ab 4";
Char * p;
Int n, n, k;
Void main () {
P=s;
While (1) {
K=sscanf (p, "% d % n, & amp; V, & amp; N);
Printf (" k, v, n=% d, % d, % d \ n ", k, v, n);
If (1)==k {
P +=n;
{} else if (0==k)
Printf (" skip char [c] % \ n ", p [0]).
P++;
} else {//EOF==k
break;
}
}
Printf (" End. \ n ");
}
//k, v, n=1123,
//k, v, n=0123,
//the skip char []
//k, v, n=0123,
//the skip char [a]
//k, v, n=0123,
//the skip char [b]
//k, v, n=1,4,2
//k, v, n=1,4,2
//End.

CodePudding user response:

Two methods, a short answer the boost library function of boost: : the split, another kind is to write their own

Want to learn can direct messages to me

CodePudding user response:

Miss zhao, the method of combining # 3 floor for reference:
 # include & lt; Stdio. H> 
Int main ()
{
Char [] s="1234124 5125126127,12,1";
Char * p;
Int n, n, k, I=0, b [10]={0};
P=s;
While (1) {
K=sscanf (p, "% d % n, & amp; V, & amp; N);
If (1)==k {
B [i++]=v;
P +=n;
{} else if (0==k)
P++;
} else {//EOF==k
break;
}
}
for(int j=0; JPrintf (" % d, b [j]);

return 0;
}
  • Related