How can I delete the first zero(s) in a string? (Without using atoi) [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.

Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}

Related

What is the meaning of the 2 in calculating the length of a substring as "(right-left+2)"? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am designing a program in C to detect tokens.
I got confused in the 4th line. What does the (right-left+2) do?
char* sub_string(char* str, int left, int right)
{
int i;
char* sub_str = (char*) malloc(sizeof(char)*(right-left+2)); /* 4th line */
}
right-left+2 in this case would be right-left+1+1.
Where righ-left would be the length from left to right, but without counting the left one. Hence one +1.
The other +1 is for making one additional character of space, most likely for adding a 0 termination.
By the way, the function is missing a clean return statement.
E.g. (thanks David C. Ranking for the idea of an example) "this" within "more this than that":
more this than that
0 5 8
The 't' is at left 5, the 's' is at right 8. The length of "this" is clearly 4 (not counting the, absent, 0 terminator). But 8-5 is only 3. So use one +1 to include the additional character (by my counting the 't' is missing...).
If that substring is to be returned via a pointer to malloced memory, which is what I guess the (missing) rest of the function is supposed to do; it should include the 0-terminator in the malloced memory. That needs another character of memory to be malloced; by using the second +1.

How to boost the process of checking two strings have the same characters in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
for example
string1='abbbc'
string2='agdee'
they have the same character 'a'
so i have to return 1;
else if like
string1'abbb'
string2'cccd'
they don't have any same character
so i have to return 0;
how to boost this searching process?
how to do this rather than using double for loop in C?
As an alternative algorithm:
Have an array of bool, size big enough to hold the highest character (e.g. ascii z is 122) you will have
Loop over the first string converting each char to its numerical and set the bool in that array index to true
Loop over the second string changing each char to numeric and using it to read the bool in that array position
If you encounter a true, return 1
if you reach the end of the array with no trues encountered return 0
This essentially converts the "wase cpu time" of checking every char against every other char (two strings of length 5, 25 comparisons) into a "burn some memory" one (two strings of length 5, up to 10 comparisons, at the expense of holding maybe 122 bools worth of memory), so there's a trade off point that varies with the length of the strings and the size of the charset.. eg two strings of length 5 that could be any Unicode.. it ain't worth burning 65k bools of memory just to save 15 ish comparisons

Finding all solutions to equation using recursion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I've been given a task to write program in C using recursion. The program is given an equation as a string, such as "123+123=246".
My task is to determine, whether the equation is right. The catch is, that numbers in the equation are sometimes replaced with a '?', for example "1??+??3=??6". Using recursion, I need to sum all possibilities, when the '?' is replaced with a digit and the equation is right.
Obviously, the solution will be based on trying out all possibilities and only selecting those, that make up the right equation. But I have no idea, how to implement it.
Could anyone give me a hint or reply with a piece of code I could base my solution on ?
Thanks very much !
Here's a general idea:
Basically we want to first of all be able to determine if the equation is right or not
Implement this function
int eval(char * string )
This will return 1 for true 0 for false and -1 when there are still '?'
Now we want write our recursion it will return a string and take a string
char * recursion (char * string)
First we need to see if the string holds a full equation.
Int res = eval(string);
if(res == 1)return string;
else if(res == 0)return "";
If it didn't stop yet that means that it can not determine because of the '?' , we need to find a way to kill them.
etch '?' can be 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9
Let's do a for loop
But first implement the method to replace the first '?' with a number,
char * Replace(char* string,int num);
After you've implemented this we create our for loop
for (int i = 0; i< 10 ; i++){
char * result =recursion(Replace(string , I));
if(Eval(result)==1) ;//we found a right answer add it to our return
}
return ""+ [all right answers we found if we even found ];
Good luck learning !

Can we include "strcmp" in printf statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Can anyone explain what is the meaning of this line of code
printf("%d",strcmp("strcmp()","strcmp()"))
The code is just a joke...
It just compares two fixed strings. To make it confusing the two strings contain the name of a library function (strcmp) but that - of cause - doesn't make it a function call.
The code
printf("%d",strcmp("strcmp()","strcmp()"))
is similar to
printf("%d",strcmp("A","A"))
It will print 0 (zero) as the strings are identical and strcmp return zero when strings have an exact compare.
strcmp() is responsible for lexicographic comparison of strings(character order).i.e.first character compared with first character if equal then second and so on.
Its signature is as follows:
int strcmp(const char *str1, const char *str2)
http://forgetcode.com/C/1026-strcmp-lexicographically-compares-two-strings
https://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm
This function return values that are as follows:
if Return value < 0 then it indicates str1 is less than str2.
if Return value > 0 then it indicates str2 is less than str1.
if Return value = 0 then it indicates str1 is equal to str2.

C - strcmp() two strings from arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am comparing two strings each coming from arrays.
while(countX<10){
if(strcmp(scanWord[countX], currentWord[countX]) == 0)
{scoreCurrent++;scoreCurrent++;}
countX++;
}
"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are. It works if I compare things that aren't those and I have printed them to screen to check too. They just don't seem to play well. Thanks in advance.
When you're reading the line, remove the newline:
char *line = fgets(currentWord[countX], 20, stdin);
if (line) {
int len = strlen(line);
if (line[len-1] == '\n') {
line[len-1] = 0;
}
}
The question as I see it:
"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are.
How do you KNOW the two values are the same? Print them out. Print out the length too. If dealing with Unicode of any sort, print out the individual bytes in hexadecimal, because two Unicode characters that look identical may actually be different.
Because the computer will never say the strings are different if they are really the same. The answer to the question as I read it is that the strings actually are different.

Resources