ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
suppose there is a string of integers, which will be 1234567, i want to add them together, so the final answer will be 1+2+3+4+5+6+7 = 28. It there a possible way to add them? this will be the input of client server program,where client enter string of integer, and server will add them up and displayed.
a simple function to do this might look like this:
Code:
int getsum(char *string, int length){
if (!string) return 0;
int sum = 0;
for (i = 0; i < length; i++) {
if (isdigit(string[i])) sum += ( string[i] - '0' );
}
return sum;
}
if that's not what you want, can you be more specific?
what do you mean by client server program?
suppose there is a string of integers, which will be 1234567, i want to add them together, so the final answer will be 1+2+3+4+5+6+7 = 28. It there a possible way to add them? this will be the input of client server program,where client enter string of integer, and server will add them up and displayed.
It looks like you want to sum a string of digits, because the entire string is an integer. What's the purpose of this particular communication between the client and the server? Have you considered separating the digits/integers with spaces? Sticking with the original question, though:
Code:
#include <stdlib.h>
int total(const char *sString)
{
char *remainder = NULL;
long value = strtol(sString, &remainder, 10);
if ((remainder && *remainder) || value < 0) return -1;
unsigned int total = 0;
do total += value % 10;
while ((value /= 10));
return total;
}
Kevin Barry
Last edited by ta0kira; 09-04-2011 at 04:21 PM.
Reason: added example
the string of integers is for client side to enter. What i want to do is when the client server has connected,client will type whatever to the server, and server will respond whatever again to the client. But what if client enter is not words but integers? If client enter like " Hello". In server it will display "Hello". If client enter "1234567". Then the server will add up the digits and display "28". That's the idea i want.I know there is something to do with the "%" thing, and i have never done the statement before. Also, i want to know how to test if the client enter a string that consist of all integers. Thank you for helping
the string of integers is for client side to enter. What i want to do is when the client server has connected,client will type whatever to the server, and server will respond whatever again to the client. But what if client enter is not words but integers? If client enter like " Hello". In server it will display "Hello". If client enter "1234567". Then the server will add up the digits and display "28". That's the idea i want.I know there is something to do with the "%" thing, and i have never done the statement before. Also, i want to know how to test if the client enter a string that consist of all integers. Thank you for helping
#include <stdlib.h>
#include <stdio.h>
int total(const char *sString)
{
char *remainder = NULL;
long value = strtol(sString, &remainder, 10);
if ((remainder && *remainder) || value < 0) return -1;
unsigned int total = 0;
do total += value % 10;
while ((value /= 10));
return total;
}
int main(int argc, char *argv[])
{
int I, value;
for (I = 1; I < argc; I++)
if ((value = total(argv[I])) < 0) printf("%s\n", argv[I]);
else printf("%i\n", value);
}
Of course, total can be replaced by something like millgates suggested, as long as it returns -1 if a non-digit is encountered.
Kevin Barry
I'm not sure how I missed this, but I named a local variabletotal within the functiontotal. Change the name of unsigned int total:
Code:
int total(const char *sString)
{
char *remainder = NULL;
long value = strtol(sString, &remainder, 10);
if((remainder && * remainder) || value < 0) return -1;
unsigned int sum = 0;
do sum += value %10;
while((value /= 10));
return sum;
}
That's probably not the problem, though. You definitely have a typo with if((value = total)argv[I])) < 0), however.
In my example I used argv just as an example source of strings to pass to total; you should pass total the lines received from the socket, but you must set the newline at the end of the line to 0 for total to work.
Kevin Barry
You should start by breaking your procedure down into smaller procedures so that the program flow makes more sense. You also can't expect to perform text communication with recv; you should fdopen the socket and fgets lines of input. I saw gets earlier and didn't notice that you used recv for the socket. You also need a condition to exit the loop when the socket is closed by the other end, not just when the other end sends "Q".
Basically you need to get to the point where reading and writing single lines at a time works reliably. After that you can do whatever you want with the lines you receive, such as totaling the digits.
Kevin Barry
hahaha. I tried some other way, but still it does not give what i want. and about you say set total to 0, i din quite get it. but i come up sumting lik this
int total(const char *sString)
{
char *remainder = NULL;
long value = strtol(sString, &remainder, 10);
if ((remainder && *remainder) || value < 0) return -1;
unsigned int sum = 0;
do sum += value % 10;
while ((value /= 10));
return sum;
}
int main(int argc, char *argv[])
{
int I, value;
int sock, connected, bytes_recieved , true = 1;
char send_data [1024] , recv_data[1024], str;
struct sockaddr_in server_addr,client_addr;
int sin_size;
I'm not actually sure what you're doing here. It looks like you're working on the server, but you're reading user input from standard input, which doesn't normally happen in a server. And where does the data go when you send it off? Where does the connection come from? You certainly don't need the loop over argv in your code, since main in my example was just setup so I could demonstrate using total both to detect non-integers and to sum digits. You should fdopen the socket immediately after a successful accept and use only that stream to read from and write to the socket since you're dealing with text. I really can't be of help with your code in its current state because I don't really know what's happening and what's supposed to be happening.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.