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 8364681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:28:50+00:00 2026-06-09T12:28:50+00:00

I am working on some string manipulation functions, just out of my own interest.

  • 0

I am working on some string manipulation functions, just out of my own interest. However, I may want to use these functions sometime in future code. I wrote the following to check if a substring exists within a string. I ran into a problem though. My program compares each char in both strings, however it had problems with out of order chars. It would take a long time to explain so I will just give an example:

if checking if “oobar” exists in the string “fooobar” my program would have trouble finding the location of the substrings because it would trip up on the first instances of the char ‘o’
I developed a work around to this, but that’s what it is a work around and not really a solid solution. So I was wondering if anybody could tell me how they would improve the following code (keep in mind I do NOT want to use any additional libraries):

int chksbstr(char *str, char *sbstr)
{
    int i, sbstrlen, strlen, p = 0;

for(i = 0; sbstr[i] != '\0'; i++);
sbstrlen = i;
for(i = 0; str[i] != '\0'; i++);
strlen = i;
if(sbstrlen > strlen)
{
    printf("\n**Error substring is larger than base string!");
    return 2;
}
if(sbstrlen == strlen)
{
    if(str == sbstr) return 0;
    else return 1;
}
for(i = 0; i <= strlen; i++)
{
    if(str[i] == sbstr[p]) p++;
    else if(str[i] != str[i - 1]) p = 0;
    if(p == sbstrlen) return 0;
}
return 1;
}
  • 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-09T12:28:51+00:00Added an answer on June 9, 2026 at 12:28 pm

    Recursion can do this better. Oh, and by the way, descriptive identifiers are way better. There’s no reason to randomly remove all the vowels, and don’t forget const.

    int check_substring(const char* str, const char* to_go, const char* substr) {
        if (*to_go == '\0') return 1; // Hit all
        if (*str == '\0') return 0; // Ran out of string to check
        if (*str == *to_go) return check_substring(str + 1, to_go + 1, substr);
        else {
            if (*str == *substr)
                return check_substring(str + 1, substr + 1, substr);
            else
                return check_substring(str + 1, substr, substr);
        }
    }
    int does_contain_substring(const char* str, const char* sbstr) {
        return check_substring(str, sbstr, sbstr);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with .NET and I want to do some string manipulations like
I am working on some string manipulation using jQuery. Basically, within a .click(function(){ I
I'm working on some code that takes a hexadecimal string input and produces the
str_replace('Ê','',$line); Ain't working. Is there some special string that represents that?
Working on some code for a machine problem; we've just started working with pointers,
I`m working on some sql queries to get some data out of a table;
I have changed tel property to do some string manipulation instead of calling that
I'm working some code that inserts csv rows into an SQLite database using Python.
I've been re-working some code due to a memory leak. The code is part
Working on some tweaks for a build script, I noticed that the output from

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.