473,395 Members | 1,936 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,395 software developers and data experts.

What's the easiest way to print the rest of the string

QQ
I have a string A[100]
I'd like to print A[offset-100]

except for for(i=offset-1;i<100;i++)
printf("%c",A[i]);

or introduce a new variable like B[100]
memcpy(B,&A[offset-1],100-offset)
printf("%s",B);
What's the easiest way to do it?

Thanks a lot!

Mar 20 '06 #1
6 1862
QQ opined:
I have a string A[100]
I'd like to print A[offset-100]
Well, this is possible only for (offset-100) within 0 and 99.
except for for(i=offset-1;i<100;i++)
printf("%c",A[i]);
This is very unclear, especially since you don't tell what `offset` is.
or introduce a new variable like B[100]
memcpy(B,&A[offset-1],100-offset)
printf("%s",B);


Can you try to rephrase this in more clear terms. I'm all confused, and
don't really understand what you want to do.

It's best if you can post a short compilable example that demonstrates
what you want to do, even if it does not work quite as you expect it.

--
BR, Vladimir

The one good thing about repeating your mistakes is that you know when
to cringe.

Mar 20 '06 #2
QQ wrote:
I have a string A[100]
I'd like to print A[offset-100]
Are you sure?

You don't say what offset is, but let's say it's a variable of
integer type with the value 20 ...

Do you want to "print A[20-100]"?
except for for(i=offset-1;i<100;i++)
printf("%c",A[i]);
If offset is 20, why do you want print starting at 19? ????
or introduce a new variable like B[100]
memcpy(B,&A[offset-1],100-offset)
printf("%s",B);
Let's exemplify with shorter numbers

#define HUNDRED 5
int main(void) {
char A[HUNDRED] = "1234";
char B[HUNDRED];
int offset = 2;

/* /--- 1 --\ /------ 3 -----\ */
memcpy(B, &A[offset - 1], HUNDRED - offset);
/* B is now {'2', '3', '4', <uninitialized>, <uninitialized> } */

printf("%s", B); /* <=== Undefined Behaviour */
return 0;
}
What's the easiest way to do it?


The easiest way for you to do it, is the one you understand and will not
have troubles modifying in three months time.

Post a small compilable program (even if it doesn't do what you want)
and explain what you expect it to output for one or two small examples.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 21 '06 #3

QQ 写道:
I have a string A[100]
I'd like to print A[offset-100]

except for for(i=offset-1;i<100;i++)
printf("%c",A[i]);

or introduce a new variable like B[100]
memcpy(B,&A[offset-1],100-offset)
printf("%s",B);
What's the easiest way to do it?

Thanks a lot!


You may do it like this:

printf("%s",&A[offset-1]); or

printf("%s", A+offset-1 );

ofcause, at the end of you string the '\0' is needed.

Mar 21 '06 #4

QQ 写道:
I have a string A[100]
I'd like to print A[offset-100]

except for for(i=offset-1;i<100;i++)
printf("%c",A[i]);

or introduce a new variable like B[100]
memcpy(B,&A[offset-1],100-offset)
printf("%s",B);
What's the easiest way to do it?

Thanks a lot!


You may do it like this:

printf("%s",&A[offset-1]); or

printf("%s", A+offset-1 );

ofcause, at the end of you string the '\0' is needed.

Mar 21 '06 #5

"QQ" <ju****@yahoo.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
I have a string A[100]
I'd like to print A[offset-100]

except for for(i=offset-1;i<100;i++)
printf("%c",A[i]);

or introduce a new variable like B[100]
memcpy(B,&A[offset-1],100-offset)
printf("%s",B);
What's the easiest way to do it?


NUL terminate the string, if you haven't, and print from &A[offset-1].

/* if A is writable, no const */
char tmp;

A[100]='\0';
printf("%s\n",&A[offset-1]);
Rod Pemberton


Mar 21 '06 #6
Pedro Graca wrote:
Let's exemplify with shorter numbers
Somehow the

#include <stdio.h>
#include <string.h>

I had at the beginning of this snippet didn't make it to the article ...
#define HUNDRED 5
int main(void) {

[snip]

/bedtime
--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 21 '06 #7

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

Similar topics

0
by: Joshua Burvill | last post by:
Hello, I am trying to print something to a print server using the following function but I get errors, does anyone have any pointers? Rgds, Josh Traceback (most recent call last): File...
30
by: Reinhold Birkenfeld | last post by:
Hello, I know that there are different YAML engines for Python out there (Syck, PyYaml, more?). Which one do you use, and why? For those of you who don't know what YAML is: visit...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
3
by: DBQueen | last post by:
I have a form with lines of controls. On some of the lines there are 3 controls (call them A,B,C); other lines have only control A. The controls have been numbered sequentially (Q20, Q21....Q76)...
8
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that...
24
by: Rhino | last post by:
I am dabbling with print CSS for the first time and I need some guidance. The web pages on my site look fine - to my untrained eye - when displayed on the monitor in any of the standard browsers....
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
16
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.