Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9295457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:36:31+00:00 2026-06-18T21:36:31+00:00

Well, the title already says what I need. I tried to use a loop

  • 0

Well, the title already says what I need. I tried to use a loop but it didn’t go well, so, I came for your help guys!

Here’s my code:

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


int main()
{
    char word[31], word2[31];
    int size1, size2;
    int i, j, k; // control
    int count = 0;

        printf ("\nInput the first word");
        scanf ("%s", word);
        printf ("\nInput the second word: ");
        scanf (" %s", word2);

// I tried  to make  a loop through the first string and if it matches a letter, it would loop through the others (if they are equal, we have a substring), but failed to put it on the `for` loop


printf ("'%s' appears %d times within '%s'", word2, count, word);

return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:36:33+00:00Added an answer on June 18, 2026 at 9:36 pm

    strstr is a useful function, it shortens your code considerably; when you find a match, just try again with the rest of the string;

    #include <string.h>
    #include <stdio.h>
    
    int main()
    {
      const char* source = "aabaa";
      const char* string2find = "aa";
    
      int occurrences;
      const char *ptr, *lastfind = NULL;
    
      for(ptr=source; (lastfind=strstr(ptr, string2find)); ptr=lastfind+1)
        occurrences++;
    
      printf("%d\n", occurrences);
    
      return 0;
    }
    

    …or if you’re really set on doing it without string.h functions, the code gets a bit more verbose;

    #include <string.h>
    #include <stdio.h>
    
    int main()
    {
      const char* source = "aaabaa";
      const char* string2find = "aa";
    
      int count=0;
      const char *position;
      for(position=source; *position; position++) {
          int comparepos, equal=1;
          for(comparepos=0; string2find[comparepos]; comparepos++) {
             if(position[comparepos] != string2find[comparepos]) {
                 equal = 0;
                 break;
             }
          }
          count+=equal;
      }
    
      printf("%d\n", count);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title explains it pretty well, but I'm trying to build a 'for loop'
well, as the title says, I need to create a component inheriting from the
I tried my best to describe the goal in the title, but here it
Well, title self describes it.. I need to run a sql function to clean
Well, question is in title. Is it possible at all? I can use css
Well, my problem is what the title says. I have build a small application
Sorry for the generic title of the question, but I didn't know how else
Well, I didn't know how to explain this one on the title, so I'll
The title pretty much says it already. I've looked through the AMD website and
I have had look at a few post already here but I am none

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.