Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to implement the strcspn function in C

1 Answer

0 votes
#include <stdio.h>

size_t strcspn_(const char* s1, const char* s2) {
	// find index in s1 of first s2[i] in s1
	const char* p1, * p2;

	for (p1 = s1; *p1 != '\0'; p1++)
		for (p2 = s2; *p2 != '\0'; p2++)
			if (*p1 == *p2) // *p2 = "am" *p1 = "ammin"
				return p1 - s1;
	return p1 - s1;
}

int main() {
	const char str1[32] = "c programming";
	const char str2[32] = "am";

	printf("%zu", strcspn_(str1, str2));

	return 0;
}




/*
run:
    
7
    
*/




 



answered Dec 18, 2022 by avibootz

Related questions

1 answer 139 views
139 views asked Jun 10, 2025 by avibootz
1 answer 191 views
1 answer 202 views
1 answer 147 views
147 views asked Aug 2, 2024 by avibootz
2 answers 289 views
289 views asked Jan 7, 2024 by avibootz
...