Reputation: 87
For example, I have a code like this:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define buff 100
.
.
.
int func(...){
char string[buff];
int i=0;
while(...){
i++;
int length_of_word_at_i =strlen(string[i]); // is this possible ? what is the proper way?
}
}
Here the 'string' array would store some words. I want to get the length of the word at an index 'i' of the string array.
Upvotes: 1
Views: 946
Reputation: 24726
As already stated in one of the other answers, you don't have to call strlen
on the address of the first character of the string. Using strlen( &string[i] )
, you can also use it on the address of a character in the middle of the string. In that case, it will return the number of remaining characters in the string.
However, in your question, you stated that your string contains several words and that you wanted the length of a word at a specific index. You did not state that you wanted the remaining length of the string. In that case, you should not use strlen
, as that function will always return the number of characters until it encounters a terminating null character. Instead, you could create your own function wordlen
, which returns the number of characters until it encounters a whitespace character or the terminating null character. This will of course only work if the words are delimitered by whitespace characters. (Your question does not state how the words are delimitered).
Here is a possible implementation of such a wordlen
function, which uses the function isspace
to determine whether a character is whitespace or not.
#include <ctype.h>
size_t wordlen( const char *str )
{
size_t len = 0;
while ( str[len] != '\0' && !isspace( (unsigned char) str[len] ) )
len++;
return len;
}
Alternatively, instead of creating your own function, you could simply use the function strcspn
, for example like this:
strcspn( &string[i], " " )
Note that this function call only considers the space character as a word delimiter, whereas the previous code example used the function isspace
, so that it also considered other types of whitespace characters as word delimiters. However, you are free to add any number of characters to the second argument, so that the function also considers these characters as delimiters.
If the words are delimitered by another character that is not whitespace, for example a comma, you can change the function call to the following:
strcspn( &string[i], "," )
Upvotes: 0
Reputation: 2304
You can do it, but keep in mind that char string[buff]
won't store an array of string, but a single string containing n buff
chars. If you would like to store strings into an array, you are looking to a char*
array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* strings[3] = {
"hello",
"hello2",
"hello123"
};
for (int i = 0; i < 3; i++)
{
int l = strlen(strings[i]);
printf("%i\n", l);
}
return 0;
}
strlen()
will help you find the length of each string contained in our char*
array as we iterate through it.
In this iteration, strings[i]
corresponds to a pointer to strings[i]
, and not its value, so it will pass the pointer to the first char
of the char*
containing your desired string to the strlen()
function, which makes it abiding to the signature of strlen()
, which requires a pointer to char as argument:
size_t strlen ( const char * str );
Output:
5
6
8
Upvotes: 2
Reputation: 409136
Remember that strlen
wants a pointer to the "first" character in the string.
If this "first" character is at index 0
or some other index doesn't matter, strlen
will happily find the null-terminator and report the length from that specific position.
So to solve your problem, use strlen(&string[i])
.
Upvotes: 3