0

the idea is to trim the string starting from the value of offset and upto the value of size and return along with the new string return 1; and if it gets outside of code boundery to simply return 0 . This is my code:

int subPrint(char *s, int offset,int size){
    char *m;
    m = (s + offset);
    while(size != 0){
     m = (m + 1);
    if(m == '\0')
        return 0;
    size--;

 }
 s = m;
return 1;

}

void subPrintTest(){
     char s[20] = "INDUSTRIAL";
     subPrint(s,2,4);
     printf("%s",s);
 }

void main(){
     subPrintTest();
     getch();
 }

The problem is that its printing the original string s unchanged

smac89
  • 39,374
  • 15
  • 132
  • 179
Ali Haroon
  • 83
  • 1
  • 4
  • 12
  • 1
    I don't see the problem, *why* should the original string change or be modified in any way? Does the program work as it should otherwise? Or maybe you should change your `subPrint` function to actually *print* something? Otherwise it's badly named. – Some programmer dude Nov 10 '16 at 19:00
  • I dont need to print it using subPrint but using subPrintTest and it doesnt need to print it all only the part I specified – Ali Haroon Nov 10 '16 at 19:03
  • So you want your function to *get* a substring and not print it? Then you need a destination to copy it to. – Some programmer dude Nov 10 '16 at 19:08
  • that why I created the new pointer m and stored what I needed in it but even though there is something missing – Ali Haroon Nov 10 '16 at 19:12

4 Answers4

0

The issue you are having is that the pointer s is being passed by value to the function subPrint. In order to do what you intend, you could pass a pointer to the pointer s.

Reference: Is passing pointer argument, pass by value in C++?

Community
  • 1
  • 1
dkach
  • 1
  • 3
0

You can keep the code you have and replace this line s = m with memcpy(destination, src, size_t) You will need to include string.h

TyAnthoney
  • 278
  • 4
  • 12
0

OP's subPrint(char *s, int offset,int size) changes some variables in the code but never affects the array pointed to by s nor prints anything.


subPrint() not needed. Use "%.*s" to specify the precision field and limit the width of the output.

char s[20] = "INDUSTRIAL";
int offset = 2;
assert(offset >= 0 && offset <= strlen(s));
int size = 4;
printf("%.*s", 4, s + 2);

Or simply

int subPrintA(char *s, int offset,int size) {
  while (offset > 0 && *s) {
    offset--;
    s++;
  }
  while (size > 0 && *s) {
    putchar(*s);
    size--;
    s++;
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • can`t use strlen as it is not allowed by the exercise I am solving – Ali Haroon Nov 10 '16 at 20:31
  • @AliHaroon Rather than put limitations like can't [use strlen](http://stackoverflow.com/questions/40535031/need-to-write-a-function-that-takes-a-string-an-integer-offset-and-size-and-prin/40536358#comment68312491_40536358) in a comment, well after posting, kinder to include that restriction in the original post. IAC, `strlen()` used here is an error check - non-essential to the code. – chux - Reinstate Monica Nov 10 '16 at 20:37
  • sorry for not mentioning this before and thx for the suggestion anyway – Ali Haroon Nov 10 '16 at 20:44
0

The problem is that you are manipulating the string pointers else where and expect the original string to be affected. Do the printing in subPrint and you should get the desired result.

int subPrint(char *s, int offset,int size) {
    char *m;
    m = (s + offset);
    while(size != 0){
     if(*m == '\0')
        return 0;
     putchar(*m); // printing is done here
     m = (m + 1);
     size--;
    }
    return 1;
}

void subPrintTest(){
     char s[20] = "INDUSTRIAL";
     subPrint(s,2,4);
}
smac89
  • 39,374
  • 15
  • 132
  • 179