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

Q

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);

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!
 
V

Vladimir S. Oka

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);


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.
 
P

Pedro Graca

QQ said:
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);


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.
 
S

stone_c

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);

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.
 
S

stone_c

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);

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.
 
R

Rod Pemberton

QQ said:
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);

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
 
P

Pedro Graca

Pedro said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top