Reputation: 5
I am a total beginner and not a native English speaker so please excuse my ignorance. My homework is to create a function in C that compares elements of char strings. The function is later called in main(), where it is supposed to work with concrete strings. But those strings might have a different size for each entry, so there is no sense in defining the size in the function. I need to know the size of each string, otherwise my program wouldn't know when to stop. How do I achieve that? My initial thought is to use the function sizeof(). Will that work or is the solution more complicated? Thanks for all answers!
Upvotes: 0
Views: 108
Reputation: 157
sizeof operator is used to evaluate size of data type or variable measured in the number of char size storage units. it will not be useful in evaluating length of the string.
for that purpose you can use strlen() function of C Library (string.h). strlen() returns the length of string without including null.
You can also write your own function for calculating length of string as follow
int stringlen(char str[])
{
int len=0;
while(str[i] != '\0')
{
len++;
}
return len;
}
Upvotes: 1