2

So I am fooling around with strchr to get part of a string from a file:

void manipulateComputers(char *name)
{
    name[strlen(name)-2] = '\0';
    printf("%s\n", name);

    char *ptr = strchr(name, ' ');
    printf("%s\n", ptr); 
}

At the first printf it reads: zelda 1 flux 1 hydra 1 willow 1 swift 1 aeon 1 neptune 1

At the second printf it reads: 1 flux 1 hydra 1 willow 1 swift 1 aeon 1 neptune 1

As you can see, the zelda is gone, which is kind of what I wanted to do. I wanted to remove the zelda, but now I want to use zelda.

Essentially, I wanted the second printf to print just zelda and not the string without zelda. How do I get a hold of that zelda to eventually pass it to another function. Thanks

Bryan
  • 117
  • 3
  • 13
  • Many methods. But it depends on how you want to return that value. Namely, modify the `name` buffer in place so that it contains only the desired string or allocate dynamic memory, copy in the desired string and return that? If you can modify `name` then just add the following after the `strchr` call: `*ptr='\0'; printf("%s\n", name);` (error checking omitted). – kaylum Jan 26 '16 at 06:04

3 Answers3

3

You've not lost zelda; the pointer name still points to it.

You can print zelda using (amongst other techniques):

int z_len = ptr - name;
printf("Zelda was here: %*.*s\n", z_len, z_len, name);

The advantage of this technique over many alternatives is that the original string is still intact — unmodified. This means it also works when the string is a const string, such as a string literal.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • @JonathanLefflet, thanks, but could you explain one thing to me please? What is going on here: %*.*s\n – Bryan Jan 26 '16 at 06:32
  • I'll refer you to [Is there a way to specify how many characters of a string to print out using `printf()`?](http://stackoverflow.com/questions/2239519/). – Jonathan Leffler Jan 26 '16 at 06:33
1

You either copy that out to another string, or you can use this trick:

char *ptr = strchr( name, ' ' );
if( ptr )
{
    char old_val = *ptr;      /* Remember the old character (in this case we know it was ' ') */
    *ptr = '\0';              /* Terminate the sub-string */
    printf( "%s\n", name );
    *ptr = old_val;           /* Restore the original character */
}
paddy
  • 60,864
  • 6
  • 61
  • 103
  • I should've asked this in the question but I thought I could figure it out. Any idea on how to move on to the 1, and then the next string. pretty much do what we did for zelda but for the entire string? – Bryan Jan 26 '16 at 07:09
  • It's not too different to just sit in a loop, and advance `ptr` until it reaches the end. You'll want an extra pointer unless you don't mind altering `name`. If you don't mind leaving the string altered, perhaps you're actually looking for [`strtok`](http://en.cppreference.com/w/c/string/byte/strtok). – paddy Jan 26 '16 at 20:26
0
#include<stdio.h>
#include<string.h>

int main()
{

char newarray[40];
char* array="zelda 1 flux 1 hydra 1 willow 1 swift 1 aeon 1 neptune 1";
printf("Orignal is :%s \n",array);
char *ptr = strchr(array, ' ');
if(ptr != NULL)
{
printf("%s\n", ptr);
if((ptr-array) < 40){
        strncpy(newarray,array,(ptr-array)*sizeof(char));
        newarray[(ptr-array)] = '\0';
        printf("Filtered string is :%s",newarray);
}
}
return 0;
}

SO here idea is you have starting address of string and now you have also the address where that part of strings ends also. So lets copy that much string in another array and use it further. When i run

./a.out 
Orignal is :zelda 1 flux 1 hydra 1 willow 1 swift 1 aeon 1 neptune 1 
 1 flux 1 hydra 1 willow 1 swift 1 aeon 1 neptune 1
Filtered string is :zelda
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222