473,320 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do we check whether a string starts with a substring?

Dear all,

In C, how do we check whether a string starts with a substring? E.g.,
char *str = ...;
char *substr = ...;

How do we check whether str starts with substr?

Thank you very much!
Jul 12 '07 #1
3 30797
scruggsy
147 100+
Dear all,

In C, how do we check whether a string starts with a substring? E.g.,
char *str = ...;
char *substr = ...;

How do we check whether str starts with substr?

Thank you very much!
Forget about doing it in C for the moment.
How would you do it, if you were looking at the two strings written side by side on a piece of paper?
Write down the steps you would take, in English, then translate it into C.
If you need help with the code, post what you have so far.
Jul 12 '07 #2
Meetee
931 Expert Mod 512MB
Dear all,

In C, how do we check whether a string starts with a substring? E.g.,
char *str = ...;
char *substr = ...;

How do we check whether str starts with substr?

Thank you very much!

Hi,

If str = "Good Morning" and substr = "Good", you can compare substr in str until white space, and if both are same purpose is solved!

Regards
Jul 12 '07 #3
If str is your orginal string, substr is the substring you want to check, then
strncmp(str, substr, strlen(substr)) will return 0 if str starts with substr. The functions strncmp and strlen are in the C header file <string.h>
Dec 8 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Paul E Collins | last post by:
Given a string variable (form input), how can I determine whether it represents a valid integer? is_numeric is true for floats as well as integers, and is_int always fails on a string. P.
3
by: Ivan Demkovitch | last post by:
Hi! I would like to check if string contain specific substring. How to do it? P.S. I've done something stupid like String.Replace
6
by: Jan | last post by:
Hi, Is there any elegant way in VB.NET to check whether a certain string is part of a "collection" of strings. Something like IF "teststring" in {"string1", "string2", "string3",...
10
by: kd | last post by:
Hi All, Is there a command to check whether a given string is present in a text file, without having to read the contents of the file, a line at a time and then check for the given string? ...
5
by: Paul Morrison | last post by:
Hi I have looked at the PHP string functions and I cant see any function to check to see whether a string starts with a certain letter or letters. Is there any way of doing this with the built...
4
by: Jason | last post by:
I have a string that looks like this? ihelloworld_zzz_yyy In a single vb.net line, I want to remove the frist character and anything after and including the first "_" resulting in : ...
8
by: varsha.gadekar | last post by:
what is the easy way to check if the given string is substring of other string only using c++?
19
by: SPABBOJU | last post by:
Hi guys... I want to check whether string does not exist with out using !~ . basical for character we check like that using . How to do the same for word, sentence ?.... I...
3
Maidenz08
by: Maidenz08 | last post by:
How do i check whether an email id exists or not? I am following a three step validation process.. 1) syntax validation- which is pretty straight forward 2) DNS validation - I'm able to do...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.