Can someone help explain what this C algorithm is doing?










0















I have been looking at this C code but am not sure what exactly it is doing. I don't understand the use of multiple if statements of finding statements.



int f(char *s) 
char *p = s;
int c = 1;
while (*p == ’ ’)
++p;
while (*p != ’’)
if ( *p < ’0’
for (--p; p >= s; --p)
if (*p == ’ ’) *p = ’0’;
*p += c;
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
if (c == 0) break;

if (c != 0)
printf("Error!n");
return 0;

return 1;









share|improve this question



















  • 1





    That is not valid C code. wandbox.org/permlink/2flW7TlwG4y6Oynz

    – Henri Menke
    Nov 15 '18 at 4:35







  • 2





    What is the general idea? Did this code fall upon you from the heavens? Didn't the place you got it from give some explanation?

    – DeiDei
    Nov 15 '18 at 4:36











  • How about you describe what the code does to us. If you get it wrong, people may give pointers. You'll learn more of use that way, than if people just told you what's going on with no effort on your part. And, BTW, what the code does it pretty simple.

    – Peter
    Nov 15 '18 at 4:47






  • 1





    Replace all with '.

    – DYZ
    Nov 15 '18 at 4:49











  • If a string contains an integer (with leading spaces) increment this integer by one. Return 1 on success and 0 on failure.

    – Henri Menke
    Nov 15 '18 at 4:50















0















I have been looking at this C code but am not sure what exactly it is doing. I don't understand the use of multiple if statements of finding statements.



int f(char *s) 
char *p = s;
int c = 1;
while (*p == ’ ’)
++p;
while (*p != ’’)
if ( *p < ’0’
for (--p; p >= s; --p)
if (*p == ’ ’) *p = ’0’;
*p += c;
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
if (c == 0) break;

if (c != 0)
printf("Error!n");
return 0;

return 1;









share|improve this question



















  • 1





    That is not valid C code. wandbox.org/permlink/2flW7TlwG4y6Oynz

    – Henri Menke
    Nov 15 '18 at 4:35







  • 2





    What is the general idea? Did this code fall upon you from the heavens? Didn't the place you got it from give some explanation?

    – DeiDei
    Nov 15 '18 at 4:36











  • How about you describe what the code does to us. If you get it wrong, people may give pointers. You'll learn more of use that way, than if people just told you what's going on with no effort on your part. And, BTW, what the code does it pretty simple.

    – Peter
    Nov 15 '18 at 4:47






  • 1





    Replace all with '.

    – DYZ
    Nov 15 '18 at 4:49











  • If a string contains an integer (with leading spaces) increment this integer by one. Return 1 on success and 0 on failure.

    – Henri Menke
    Nov 15 '18 at 4:50













0












0








0








I have been looking at this C code but am not sure what exactly it is doing. I don't understand the use of multiple if statements of finding statements.



int f(char *s) 
char *p = s;
int c = 1;
while (*p == ’ ’)
++p;
while (*p != ’’)
if ( *p < ’0’
for (--p; p >= s; --p)
if (*p == ’ ’) *p = ’0’;
*p += c;
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
if (c == 0) break;

if (c != 0)
printf("Error!n");
return 0;

return 1;









share|improve this question
















I have been looking at this C code but am not sure what exactly it is doing. I don't understand the use of multiple if statements of finding statements.



int f(char *s) 
char *p = s;
int c = 1;
while (*p == ’ ’)
++p;
while (*p != ’’)
if ( *p < ’0’
for (--p; p >= s; --p)
if (*p == ’ ’) *p = ’0’;
*p += c;
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
if (c == 0) break;

if (c != 0)
printf("Error!n");
return 0;

return 1;






c string algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 4:44









DYZ

27k62049




27k62049










asked Nov 15 '18 at 4:28









Sarthak RakhraSarthak Rakhra

41




41







  • 1





    That is not valid C code. wandbox.org/permlink/2flW7TlwG4y6Oynz

    – Henri Menke
    Nov 15 '18 at 4:35







  • 2





    What is the general idea? Did this code fall upon you from the heavens? Didn't the place you got it from give some explanation?

    – DeiDei
    Nov 15 '18 at 4:36











  • How about you describe what the code does to us. If you get it wrong, people may give pointers. You'll learn more of use that way, than if people just told you what's going on with no effort on your part. And, BTW, what the code does it pretty simple.

    – Peter
    Nov 15 '18 at 4:47






  • 1





    Replace all with '.

    – DYZ
    Nov 15 '18 at 4:49











  • If a string contains an integer (with leading spaces) increment this integer by one. Return 1 on success and 0 on failure.

    – Henri Menke
    Nov 15 '18 at 4:50












  • 1





    That is not valid C code. wandbox.org/permlink/2flW7TlwG4y6Oynz

    – Henri Menke
    Nov 15 '18 at 4:35







  • 2





    What is the general idea? Did this code fall upon you from the heavens? Didn't the place you got it from give some explanation?

    – DeiDei
    Nov 15 '18 at 4:36











  • How about you describe what the code does to us. If you get it wrong, people may give pointers. You'll learn more of use that way, than if people just told you what's going on with no effort on your part. And, BTW, what the code does it pretty simple.

    – Peter
    Nov 15 '18 at 4:47






  • 1





    Replace all with '.

    – DYZ
    Nov 15 '18 at 4:49











  • If a string contains an integer (with leading spaces) increment this integer by one. Return 1 on success and 0 on failure.

    – Henri Menke
    Nov 15 '18 at 4:50







1




1





That is not valid C code. wandbox.org/permlink/2flW7TlwG4y6Oynz

– Henri Menke
Nov 15 '18 at 4:35






That is not valid C code. wandbox.org/permlink/2flW7TlwG4y6Oynz

– Henri Menke
Nov 15 '18 at 4:35





2




2





What is the general idea? Did this code fall upon you from the heavens? Didn't the place you got it from give some explanation?

– DeiDei
Nov 15 '18 at 4:36





What is the general idea? Did this code fall upon you from the heavens? Didn't the place you got it from give some explanation?

– DeiDei
Nov 15 '18 at 4:36













How about you describe what the code does to us. If you get it wrong, people may give pointers. You'll learn more of use that way, than if people just told you what's going on with no effort on your part. And, BTW, what the code does it pretty simple.

– Peter
Nov 15 '18 at 4:47





How about you describe what the code does to us. If you get it wrong, people may give pointers. You'll learn more of use that way, than if people just told you what's going on with no effort on your part. And, BTW, what the code does it pretty simple.

– Peter
Nov 15 '18 at 4:47




1




1





Replace all with '.

– DYZ
Nov 15 '18 at 4:49





Replace all with '.

– DYZ
Nov 15 '18 at 4:49













If a string contains an integer (with leading spaces) increment this integer by one. Return 1 on success and 0 on failure.

– Henri Menke
Nov 15 '18 at 4:50





If a string contains an integer (with leading spaces) increment this integer by one. Return 1 on success and 0 on failure.

– Henri Menke
Nov 15 '18 at 4:50












1 Answer
1






active

oldest

votes


















1














// return an integer given a character pointer, a string.
int f(char *s)
// Set current position to start of string
char *p = s;
// Initialise carry flag to '1'.
int c = 1;
// Move position past leading spaces
while (*p == ’ ’)
++p;
// Check remaining characters are in the set '0','1',..,'9'
while (*p != ’’) *p > ’9’ )
printf("Error!n"); return 0;

++p;
// Now counting back from the end of the string
for (--p; p >= s; --p)
// Turn a space into a '0';
if (*p == ’ ’) *p = ’0’;
// Increment the digit by the value of the carry; one or zero
*p += c;
// This might cause a further carry, capture that
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
// if no carry, break, else keep on with the carry
if (c == 0) break;

// If still carrying passed the end of the space, call an error.
if (c != 0)
printf("Error!n");
return 0;

return 1;


Essentially: If the input is a digit string, add one; might need a leading space and will use it if the input is all '9's.






share|improve this answer




















  • 1





    The whole for (--p; p >= s; --p) loop reduces to *(--p) += 1; c = 0; by the way.

    – Henri Menke
    Nov 15 '18 at 4:48












  • @HenriMenke Thanks. Seem to have managed to read the critical line backwards; now makes some sense.

    – Keith
    Nov 15 '18 at 5:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312445%2fcan-someone-help-explain-what-this-c-algorithm-is-doing%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














// return an integer given a character pointer, a string.
int f(char *s)
// Set current position to start of string
char *p = s;
// Initialise carry flag to '1'.
int c = 1;
// Move position past leading spaces
while (*p == ’ ’)
++p;
// Check remaining characters are in the set '0','1',..,'9'
while (*p != ’’) *p > ’9’ )
printf("Error!n"); return 0;

++p;
// Now counting back from the end of the string
for (--p; p >= s; --p)
// Turn a space into a '0';
if (*p == ’ ’) *p = ’0’;
// Increment the digit by the value of the carry; one or zero
*p += c;
// This might cause a further carry, capture that
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
// if no carry, break, else keep on with the carry
if (c == 0) break;

// If still carrying passed the end of the space, call an error.
if (c != 0)
printf("Error!n");
return 0;

return 1;


Essentially: If the input is a digit string, add one; might need a leading space and will use it if the input is all '9's.






share|improve this answer




















  • 1





    The whole for (--p; p >= s; --p) loop reduces to *(--p) += 1; c = 0; by the way.

    – Henri Menke
    Nov 15 '18 at 4:48












  • @HenriMenke Thanks. Seem to have managed to read the critical line backwards; now makes some sense.

    – Keith
    Nov 15 '18 at 5:36















1














// return an integer given a character pointer, a string.
int f(char *s)
// Set current position to start of string
char *p = s;
// Initialise carry flag to '1'.
int c = 1;
// Move position past leading spaces
while (*p == ’ ’)
++p;
// Check remaining characters are in the set '0','1',..,'9'
while (*p != ’’) *p > ’9’ )
printf("Error!n"); return 0;

++p;
// Now counting back from the end of the string
for (--p; p >= s; --p)
// Turn a space into a '0';
if (*p == ’ ’) *p = ’0’;
// Increment the digit by the value of the carry; one or zero
*p += c;
// This might cause a further carry, capture that
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
// if no carry, break, else keep on with the carry
if (c == 0) break;

// If still carrying passed the end of the space, call an error.
if (c != 0)
printf("Error!n");
return 0;

return 1;


Essentially: If the input is a digit string, add one; might need a leading space and will use it if the input is all '9's.






share|improve this answer




















  • 1





    The whole for (--p; p >= s; --p) loop reduces to *(--p) += 1; c = 0; by the way.

    – Henri Menke
    Nov 15 '18 at 4:48












  • @HenriMenke Thanks. Seem to have managed to read the critical line backwards; now makes some sense.

    – Keith
    Nov 15 '18 at 5:36













1












1








1







// return an integer given a character pointer, a string.
int f(char *s)
// Set current position to start of string
char *p = s;
// Initialise carry flag to '1'.
int c = 1;
// Move position past leading spaces
while (*p == ’ ’)
++p;
// Check remaining characters are in the set '0','1',..,'9'
while (*p != ’’) *p > ’9’ )
printf("Error!n"); return 0;

++p;
// Now counting back from the end of the string
for (--p; p >= s; --p)
// Turn a space into a '0';
if (*p == ’ ’) *p = ’0’;
// Increment the digit by the value of the carry; one or zero
*p += c;
// This might cause a further carry, capture that
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
// if no carry, break, else keep on with the carry
if (c == 0) break;

// If still carrying passed the end of the space, call an error.
if (c != 0)
printf("Error!n");
return 0;

return 1;


Essentially: If the input is a digit string, add one; might need a leading space and will use it if the input is all '9's.






share|improve this answer















// return an integer given a character pointer, a string.
int f(char *s)
// Set current position to start of string
char *p = s;
// Initialise carry flag to '1'.
int c = 1;
// Move position past leading spaces
while (*p == ’ ’)
++p;
// Check remaining characters are in the set '0','1',..,'9'
while (*p != ’’) *p > ’9’ )
printf("Error!n"); return 0;

++p;
// Now counting back from the end of the string
for (--p; p >= s; --p)
// Turn a space into a '0';
if (*p == ’ ’) *p = ’0’;
// Increment the digit by the value of the carry; one or zero
*p += c;
// This might cause a further carry, capture that
if (*p > ’9’)
*p = ’0’; c = 1;
else
c = 0;
// if no carry, break, else keep on with the carry
if (c == 0) break;

// If still carrying passed the end of the space, call an error.
if (c != 0)
printf("Error!n");
return 0;

return 1;


Essentially: If the input is a digit string, add one; might need a leading space and will use it if the input is all '9's.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 5:35

























answered Nov 15 '18 at 4:46









KeithKeith

6,3391422




6,3391422







  • 1





    The whole for (--p; p >= s; --p) loop reduces to *(--p) += 1; c = 0; by the way.

    – Henri Menke
    Nov 15 '18 at 4:48












  • @HenriMenke Thanks. Seem to have managed to read the critical line backwards; now makes some sense.

    – Keith
    Nov 15 '18 at 5:36












  • 1





    The whole for (--p; p >= s; --p) loop reduces to *(--p) += 1; c = 0; by the way.

    – Henri Menke
    Nov 15 '18 at 4:48












  • @HenriMenke Thanks. Seem to have managed to read the critical line backwards; now makes some sense.

    – Keith
    Nov 15 '18 at 5:36







1




1





The whole for (--p; p >= s; --p) loop reduces to *(--p) += 1; c = 0; by the way.

– Henri Menke
Nov 15 '18 at 4:48






The whole for (--p; p >= s; --p) loop reduces to *(--p) += 1; c = 0; by the way.

– Henri Menke
Nov 15 '18 at 4:48














@HenriMenke Thanks. Seem to have managed to read the critical line backwards; now makes some sense.

– Keith
Nov 15 '18 at 5:36





@HenriMenke Thanks. Seem to have managed to read the critical line backwards; now makes some sense.

– Keith
Nov 15 '18 at 5:36



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312445%2fcan-someone-help-explain-what-this-c-algorithm-is-doing%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto