473,463 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

return the start of a substring in a string in c

Hi,
In java, there is an 'indexOf' function in String which does this:

indexOf(String str)
Returns the index within this string of the first occurrence
of the specified substring.

is there anything like that in c?

The closest thing I can find is strchr, but it check for a character,
not a substring?

Thank you.

Jul 14 '07
53 3347
Malcolm McLean wrote On 07/19/07 18:14,:

(... *Still* declining to defend his claim that strstr()
and its caller "talk to each other" with integers ...)
"Eric Sosman" <Er*********@sun.comwrote in message
news:1184880234.495526@news1nwk...
> And what language do you propose to use to manipulate
those samples? Not 64-bit-only C, that's for sure. Here's
my way of reducing the volume of one sample:

uint16_t *sample = ...;
*sample -= *sample / 10;

... and here's what you want me to do instead:

/* 8-bit (?) */ unsigned char *samplebytes = ...;
/* 64-bit */ unsigned int sample;
sample = samplebytes[0] + (samplebytes[1] << 8);
sample -= sample / 10;
samplebytes[0] = sample;
samplebytes[1] = sample >8;

It won't wash, Malcolm. It wouldn't wash even if Herakles
ran a couple rivers over it.


/* 64 bit int only code */

void reduce10(unsigned char *audio, int N)
{
int i;
int sample;

for(i=0;i<N;i++)
{
sample = get16(audio + i * 2);
sample -= sample/10;
put16(audio + i * 2, sample);
}
}

#define get16(ptr) ( ( (int)ptr[0] << 8) + ptr[1])
#define put16(ptr, x) do{ptr[0] = (x >>8) & 0xFF; ptr[1] = x &
0xFF;}while(0)

The last macro is a bit of nuisance, I'd be the first to agree. You'll use
these all the time when writing samples to and from the buffer. It might
just make a difference for such a simple manipulation as knocking off 10%.
Do you think you *improve* things by taking one simple
line of code and obfuscating it with an intermediate variable
and two macros (both unsafe from side-effects)?
Compare

void up10(uint16_t *sample, size_t N)
{
size_t i;

for(i=0;i<N;i++)
sample[i] += sample[i]/10;
}

Superfically this looks OK, but here are a few ways to break it.

up10(boomingadvert, N);
If you're worried about clipping, observe that using
64 bits won't fix it. All it'll do is postpone the clip
until the point when the samples are chopped to 16 bits
for burning to the CD. (Or do you propose that CDs should
record 64-bit samples, too? You want to cut the playing
time to from 80 to 20 minutes? One LP side = 2 CD64's?)

I'll admit one small advantage: By using lots of extra
bits the sample would survive amplification followed by a
subsequent diminution: up by ten percent and then back down
by fifty wouldn't clip. But that's not how you *do* sound
editing: You boost the volume, say "Oops! It clipped," hit
the Undo button, and boost again by a smaller amount.
up10(sample, N - window);
I don't understand what breakage you suggest here,
nor how 64-bit ints would cure it.
short *samples; /* from your colleague Fred */
up10((uint16_t *) samples, N);
Fred gave me the wrong stuff. If he gave the same
wrong stuff to you, you'd be equally in the soup.

--
Er*********@sun.com
Jul 19 '07 #51
"Malcolm McLean" <re*******@btinternet.comwrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message
They are all just whole numbers. However on a 64
bit machine, 64 bit integers are efficient.
And we all have 64 bit machines, right? Guess what: wrong.
The campaign for 64 bit ints campaigns for 64 bit ints on 64 bit machines,
maintaining the convention that int is the natural integer type for the
platform. On 32 bit machines it is accepted that int should be 32 bits, just
as it should be 16 bits on small machines with 16 bit registers.

The idea is that an integer can be used to index any array.
So you _still_ haven't grasped the difference between an int and an
integer.

Richard
Jul 20 '07 #52

"Eric Sosman" <Er*********@sun.comwrote in message
news:1184884531.511019@news1nwk...
Malcolm McLean wrote On 07/19/07 18:14,:

(... *Still* declining to defend his claim that strstr()
and its caller "talk to each other" with integers ...)
|No. Functions talk to each other via integers, just as humans talk to each
other via soundwaves. That isn't the only means of communication, of
course - we're not using soundwaves now - but it's an important one.
Similarly not every function takes integer parameters, and of those only a
subset take integers by indirection, which is the real problem. However that
set is large enough for integer representation to be a real issue.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jul 20 '07 #53
Malcolm McLean wrote On 07/20/07 17:24,:
"Eric Sosman" <Er*********@sun.comwrote in message
news:1184884531.511019@news1nwk...
>>Malcolm McLean wrote On 07/19/07 18:14,:

(... *Still* declining to defend his claim that strstr()
and its caller "talk to each other" with integers ...)

|No. Functions talk to each other via integers, just as humans talk to each
other via soundwaves. That isn't the only means of communication, of
course - we're not using soundwaves now - but it's an important one.
Similarly not every function takes integer parameters, and of those only a
subset take integers by indirection, which is the real problem. However that
set is large enough for integer representation to be a real issue.
Your original claim was

"The difference is that the integer representation
is the way that functions talk to each other."
-- Malcolm McLean, 2007-07-17

Not "a way" nor "one of the ways," but "the way." The
claim is, of course, absurd, and I'm relieved to see
that you're willing to drop it, even if it took three
days to reach that point.

May we hope that you will also drop this silly "One
size fits all" nonsense? The past does not encourage me
to be optimistic, but even a dim hope beats despair.

--
Er*********@sun.com

Jul 20 '07 #54

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher-Robin | last post by:
Hi I've got a problem and I hope someone can help me: I want a PHP script to call another PHP script, execute it and return its OUTPUT (not its content) into a STRING in the first script. ...
0
by: KENNY L. CHEN | last post by:
I have a table with a CHAR column which stored a COBOL redefined record. I tried to retrieve part of the field from the column with Oracle substr function. Here is one of example: The...
3
by: Bob Maggio | last post by:
I have created a function that returns a string containing raw HTML to be used on a web form (see below): private string GetReportHTML() { string strHTML = ""; // I have some code here that...
0
by: leekent | last post by:
I need to write an activeX object to be installed on client machine which will be called using vbscript in web pages. The activeX will be writen in C#, and I need to return an array of strings. I...
2
by: sys | last post by:
I have a web method that returns a string. When I return a string that has a carriage return and line feed in it ("\r\n") on the client side I only receive a string including the line feed ("\n")....
10
by: howa | last post by:
#include <iostream> using namespace std; const char* test(char *src) { string s(src); return s.c_str(); }
3
by: lostbuttrying | last post by:
This is the exact question given...I wish I could start or make a stab, but I don't know where to begin: Submit a Python file named, 'countDemo.py', that defines and demonstrates a function that...
4
by: kalaisuresh | last post by:
Hi, I want to remove the carriage return from the string using c++. please anybody tell me. i am reading the string from another file.(each string is a single line);
3
by: sam_cit | last post by:
Hi Everyone, I have the following function, char* sample1() { char *p = "india"; return(p); }
11
by: nembo kid | last post by:
My homework is to make a simple function that, given a unsigned char, should returns its bit string representation. I tried to drop down some code (as follows) but it doesn't work. Compiling is...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.