How can I write my own version of the C standard function strspn?

In summary, the conversation is about the implementation of a custom version of the C standard function strspn. The function is supposed to return the number of characters in str2 that exist in str1. However, the code provided is not working and there are some issues with the logic and initialization of variables. It is suggested to reset the pointer k and only count the number of unique letters instead of the total number of occurrences.
  • #1
James889
192
1
Hi,

im trying to write my own version of the C standard function strspn.

It returns the number of characters in str2 that exists in str1.

I cannot get mine to work.

Code:
#include <stdio.h>
#include <stdlib.h>

int strspn(const char *str1, const char *str2){

int n;
const char *s,*k;
s = str1;
k = str2;

while(*s++ != '\0'){
   for(; *k++ != '\0';){
     
if(*s == *k)
n++;
}
}
           
return n;                     
}

if i try it with something like strspn("1214bk","1214"), it returns 16386...
:(
 
Physics news on Phys.org
  • #2
Well, for starters you never initialize the counter n. Secondly, you need to "reset" k (i.e. make k point to str2) when s is incremented. Also, if the two strings for e.g. "asd" and "asdasd", then should strprn return 3 or 6? I.e., does it only see if the letters exist, and not how many times? In your example, it returns 6.

It could be a good exercise to implement a method, such that it only counts the number of letters occurring in both words, and not how many times (i.e. so it returns 3, and not 6).
 
Last edited:

1. What does the "strspn" function do?

The "strspn" function is a C standard library function that calculates the length of the initial segment of a string that consists entirely of characters contained within a specified set of characters.

2. How is the "strspn" function used?

The "strspn" function is used by providing two arguments: the string to be searched and the set of characters to search for. It then returns the number of characters in the initial segment of the string that match any of the characters in the set.

3. Can the "strspn" function be used to compare two strings?

No, the "strspn" function is used to compare a string to a set of characters. To compare two strings, the "strcmp" function should be used instead.

4. What happens if the string being searched is empty?

If the string being searched is empty, the "strspn" function will return a value of 0, as there are no characters in the initial segment that match the set of characters being searched for.

5. Are there any potential errors or issues when using the "strspn" function?

Yes, there are a few potential errors or issues that may arise when using the "strspn" function. One common issue is forgetting to include the null terminator character in the set of characters being searched for, which can lead to unexpected results. Additionally, the function may return an incorrect value if the string being searched or the set of characters being searched for is not properly null-terminated.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
946
  • Engineering and Comp Sci Homework Help
Replies
3
Views
944
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
999
  • Engineering and Comp Sci Homework Help
Replies
3
Views
887
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top