logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C Program To Count the Occurence of a Substring in String

Posted on April 4, 2013 by staff10

This is a C Program to count the occurrence of a substring in string.

Problem Description

This program takes a string and a substring as input and counts the occurrence of a substring in string.

Problem Solution

1. Take a string and a substring as input.
2. Firstly check for the substring in the string.
3. If it is present then count the number of times it is present.

advertisement
Program/Source Code

Here is source code of the C Program to count occurrence of a substring in string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program To Count the Occurrence of a Substring in String 
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. char str[100], sub[100];
  8. int count = 0, count1 = 0;
  9.  
  10. void main()
  11. {
  12.     int i, j, l, l1, l2;
  13.  
  14.     printf("\nEnter a string : ");
  15.     scanf("%[^\n]s", str);
  16.  
  17.     l1 = strlen(str);
  18.  
  19.     printf("\nEnter a substring : ");
  20.     scanf(" %[^\n]s", sub);
  21.  
  22.     l2 = strlen(sub);
  23.  
  24.     for (i = 0; i < l1;)
  25.     {
  26.         j = 0;
  27.         count = 0;
  28.         while ((str[i] == sub[j]))
  29.         {
  30.             count++;
  31.             i++;
  32.             j++;
  33.         }
  34.         if (count == l2)
  35.         {
  36.             count1++;                                   
  37.             count = 0;
  38.         }
  39.         else
  40.             i++;
  41.     }    
  42.     printf("%s occurs %d times in %s", sub, count1, str);
  43. }
Program Explanation

1. Take a string and a substring as input and store it in the array str and sub respectively.
2. Find the length of both the strings using strlen function.
3. Using for loop find whether the substring is present or not. If it is present then count the number of times it is present using the variable count.
4. Print the variable count as output.

advertisement
Runtime Test Cases
 
 
Enter a string : prrrogram c prrrogramming
 
Enter a substring : rr
rr occurs 2 times in prrrogram c prrrogramming

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Implement Depth First Search Traversal using Post Order
» Next Page - C Program to Find Sum of Numbers given in Command Line Arguments Recursively

« C Program to Implement Depth First Search Traversal using Post Order
C Program to Find Sum of Numbers given in Command Line Arguments Recursively »

Deep Dive @ Sanfoundry:

  1. C# Programming Examples
  2. C Algorithms, Problems & Programming Examples
  3. C++ Algorithms, Problems & Programming Examples
  4. C Programming Examples
  5. C Programming Examples on Bitwise Operations
  6. Python Programming Examples
  7. C++ Programming Examples on Set & String Problems & Algorithms
  8. C# Programming Examples on Strings
  9. C Programming Examples on Set & String Problems & Algorithms
  10. C Programming Examples on Strings
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.