C counterpart to C++ find_first_not_of? - c

Is there a C function which can do the equivalent of find_first_not_of, receiving a string to search and a set of characters and returning the first character in the string that's not of the set?

The strspn function will get you most of the way there. (You just have to massage the return value a bit.)

Related

Differnce between gcvt() and _gcvt() in C

I would like to convert a double into character string and I find gcvt() and _gcvt(). I just wondering what is the difference between them. Both return with char* and both need value, number of digits and buffer as a given parameters
As per the google search result
The _gcvt() function is identical to gcvt(). Use _gcvt() for ANSI/ISO naming conventions.

how to pass null character to the c gets function? or terminate the input with a \0 character

I am trying to reverse engineer a binary file
now its needed that i have to pass a long string such as
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\0"
and it has to be terminated with a null character at last
how to pass the null character to the Puts function?
please guide me
Thanks in advance
Your question is quite unclear. What exactly do you want to do?
In nearly all C functions strings are "NUL terminated". This means that there is no information about the length of a string but the "NUL" byte at the end.
And the C compiler automatically adds a NUL byte at the end of string constants.
Example: puts("Hello") actually means: puts("Hello\0").
So if you want to know how to pass a NUL-terminated string to a C function: Simply pass the string to the C function; the compiler will add the NUL byte automatically.
The function puts has no chance to find out the "real" length of the string if there are "NUL" characters inside the string: puts("Hello\0 world") is the same as puts("Hello").
So if you want to write a "NUL" byte to the output, you have to use a function that does not take a NUL-terminated string. You might use the write or the fwrite function.
Example: Use fwrite("Hello\0 world\n",1,13,stdout) instead of puts("Hello\0 world")

How can I check if string contains character using ASSERT?

How can I check if string contains character using ASSERT? For example, I want to do something with my string only if I have '+' in it. How can I do using assert in C?
You can use strchr along with assert function.
char x[20] = "hello+";
assert(strchr(x,'+') != NULL);
strchr(x,'+') will return NULL if character is not found in the string hence you can use the its return value to assert.
According to assert man page
If expression is false (i.e., compares equal to zero), assert()
prints an error message to standard error and terminates the program
by calling abort(3). The error message includes the name of the file
and function containing the assert() call, the source code line
number of the call, and the text of the argument; something like:
And according to strchr man page
The strchr() and strrchr() functions return a pointer to the matched
character or NULL if the character is not found. The terminating
null byte is considered part of the string, so that if c is specified
as '\0', these functions return a pointer to the terminator.

Why do we use 'strlen()' if 'printf()' itself returns the length of the string?

The C function printf() returns the no of characters printed then what makes strlen() different?
As bub said, part of the difference is that strlen doesn't print the string, just returns its length. Also, though, printf() interprets its argument as a format string - what it ends up printing can be a very different length from the string it was given if it inserts values into placeholders.
strlen() function calculates the length of string. Thats it for strlen but printf() sends formatted output.

Tool functions for chars

I want to handle some char variables and would like to get a list of some functions that can do these tasks when it comes to handling chars.
Getting first characters of a char (var_name[1] doesnt seem to work)
Getting last characters of a char
Checking for char1 matches with char2 ( eg if "unicorn" matches words with "bicycle"
I am pretty sure some of these methods exist in libraries such as stdio.h or so but google isnt my friend.
EDIT:My 3rd question means not direct match with strcmp but single character match(eg if "hey" and "hello") have e as common letter.
Use var_name[0] to get first character (array indexes run from 0 to N - 1, where N is the number of elements in the array).
Use var_name[strlen(var_name) - 1] to get the last character.
Use strcmp() to compare two char strings.
EDIT:
To search for character in a string you can use strchr():
if (strchr("hello", 'e') && strchr("hey", 'e'))
{
}
There is also strpbrk() function that would indicate if two strings have any common characters:
if (strpbrk("hello", "hey"))
{
}
Assuming you mean a char[], and not a char which is a single character.
C uses 0-based indexing, var_name[0] gives you the first char.
strlen() gives you the length of the string, which together with my answer to 1. means
char lastchar = var_name[strlen(var_name)-1]; http://www.cplusplus.com/reference/clibrary/cstring/strlen/
strcmp(var_name1, var_name2) == 0. http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
I am pretty sure some of these methods exist in libraries such as
stdio.h or so but google isnt my friend.
The string functions in the C standard library (libc) are described in the header file . If you're on a unix-ish machine, try typing man 3 string at a command line. You can then use the man program again to get more information about specific functions, e.g. man 3 strlen. (The '3' just tells man to look in "section 3", which describes the C standard library functions.)
What you're looking for is the string functions in the C runtime library. These are defined in string.h, not stdio.h.
But your list of problems is simple:
var_name[0] works perfectly well for accessing the first char in an array. var_name[ 1] doesn't work because arrays in C are zero-based.
The last char in an array is:
char c;
c = var_name[strlen(var_name)-1];
Testing for equality is simple:
if (var_name[0] == var_name[1])
; // they match
C and C++ strings are zero indexed. The memory you need to hold a particular length string has to be at least the string length and one character for the string terminator \0. So, the first character is array[0].
As #Carey Gregory said, the basic string handling functions are in string.h. But these are only primitives for handling strings. C is a low level enough language, that you have an opportunity to build up your own string handling library based on the functions in string.h.
On example might be that you want to pass a string pointer to a function and also the length of the buffer holding that sane string, not just the string length itself.

Resources