710 questions
3 votes
3 answers
202 views
strlen() returns 1 larger on linux than windows
I am building a c extension for a python module and it needs to work the same on a windows machine and on a raspberry pi. #include <stdio.h> #include <string.h> #include <ctype.h> #...
3 votes
5 answers
265 views
Why does strlen(NULL) cause a compile-time warning, while my custom ft_strlen(NULL) only crashes at runtime?
I'm trying to reimplement the standard strlen function in C, and I'm closely replicating its behavior. I defined my function based on standard C declaration for strlen: size_t ft_strlen(const char *s);...
0 votes
2 answers
156 views
Why the while loop is not executing with strlen(sentence) - 1?
The code is supposed to reverse the words that have the length equal to longest word length from the phrase. Input string: #Voi#da#bacu#la#info# Expected output string: #Voi#da#ucab#la#ofni# I ...
1 vote
4 answers
192 views
Why does \0 not affect the length of a string in C?
Consider this example where I add some extra \0 to a string. #include <stdio.h> #include <string.h> int main(int argc, char **argv){ char str1[] = "dog"; char str2[] = &...
0 votes
2 answers
116 views
C strlen() returns the wrong String length (character count) when using umlauts [duplicate]
I'm working on a C program, which takes an input in form of a parameter and then needs the length of the string to continue its work. Unfortunately I'm facing a massive issue in form of a wrong ...
1 vote
2 answers
75 views
Unable to get length of arg - C [closed]
basically I'm trying to get the length (using strlen) of an argument, but i keep getting this error. From what I've understood it means that I'm giving an array of pointers to char arrays instead of a ...
2 votes
3 answers
217 views
Why is the strlen here equal to 25?
Just so I can confirm if what I think is going on is really going on. The following code prints out 25 when I give it the (26 letter) alphabet as an input, is it because fgets always automatically ...
2 votes
1 answer
164 views
Why doesn't the compiler optimize strlen() calls in a loop, despite it being a pure function? And how would [[reproducible]] affect this?
I have two functions that convert a string to lowercase. The first one calls strlen() in every iteration, while the second one only calls it after modifying the string. I expected the compiler to ...
2 votes
3 answers
247 views
C strlen on char array
I'm new to programming, so I was practicing with C. Then I ran to a problem with my code: #include <stdio.h> #include <string.h> int main() { char a[] = "Hello"; printf(&...
0 votes
1 answer
91 views
User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
I am trying to write a little program, which is resistant against buffer overflow and similar vulnerabilities. Since we cannot trust the user input, I thought it would be a good idea to concatenate ...
4 votes
0 answers
103 views
Is there a better way to search any byte from a long value?
In OPENJDK's StringSupport class, there are following codes checking if there could be a '\0' byte in a long value: private static final long HIMAGIC_FOR_BYTES = 0x8080_8080_8080_8080L; private static ...
3 votes
1 answer
194 views
Strlen function giving wrong length when there are non-english characters in string
I have a program that accepts non-english characters also as an input field. Because we use strlen, it has failed to give expected length while calculating the length of the string when there is a non-...
0 votes
0 answers
108 views
add_action hook is not working on user_register in wordpress
I have a wordpress site, and my registration url is Registration URL. I have registered a hook to modify user_login after registration if username is more than 10 digits. if user given mobile number ...
1 vote
1 answer
112 views
c++, How to display an error if user inputs more than one char
I have been trying to find a solution for my school project. The program should display an invalid error if the user enters "Y Y". Currently the program still accepts it and moves on to the ...
0 votes
1 answer
177 views
Why +1 in realloc for string in C? If it's for null terminator, then how does strlen() measures len of the str if there's no null byte at the end? [duplicate]
This code is from a problem in HackerRank (Q: Printing Tokens in C). #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *s = malloc(1024 * sizeof(char))...