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

  • Thread starter Thread starter James889
  • Start date Start date
  • Tags Tags
    Function Standard
Click For Summary
SUMMARY

The forum discussion focuses on creating a custom implementation of the C standard library function strspn. The user encountered issues with their code, particularly with an uninitialized counter variable n and the need to reset the pointer k for each character in str1. The correct behavior of strspn is clarified, emphasizing that it should count the number of characters in str2 that exist in str1, not the total occurrences. The discussion also raises a question about whether to count unique characters or total occurrences.

PREREQUISITES
  • Understanding of C programming language syntax and semantics
  • Familiarity with pointer manipulation in C
  • Knowledge of string handling functions in C
  • Basic debugging skills in C to identify logical errors
NEXT STEPS
  • Implement a corrected version of strspn that initializes the counter and resets pointers correctly
  • Research the differences between counting unique characters versus total occurrences in string functions
  • Explore other string manipulation functions in the C standard library for comparison
  • Learn about unit testing in C to validate the functionality of custom string functions
USEFUL FOR

C programmers, software developers working with string manipulation, and students learning about C standard library functions will benefit from this discussion.

James889
Messages
190
Reaction score
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
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 21 ·
Replies
21
Views
5K
  • · Replies 24 ·
Replies
24
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
7
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K