My problem is that i want to get the 1st part only of the string ive done based on my code i can get the audio/x-aiff by finding the space but want i want to do is i want to is get or print the .aif is there any way to get that 1st substring thanks in advance

#include <stdio.h>
#include <string.h>
 
int main(void) 
{
        char *cptr;
        char str[] = ".aif: audio/x-aiff";
        char substr[] = " ";
 
        cptr = strstr(str, substr);
 
        printf("%s\n", cptr);
 
        return 0;
}

Recommended Answers

All 3 Replies

You can easily get a pointer to that location through the str variable. However, printing it without the remainder of the string will involve either a copy or modification of the original string to insert a null terminator.
Copying the string would look something like (updating your original example):

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

int main(void)
{
    char *cptr, *begin;
    char str[] = ".aif: audio/x-aiff";
    char substr[] = " ";
    char front[10] = {0};

    begin = str;
    cptr = strstr(str, substr);
    strncpy (front, str, cptr - begin);

    printf ("%s\n", front);
    printf("%s\n", cptr);

    return 0;
}
#include <stdio.h>
#include <string.h> 
int main(void)
 {
        int i ;
        i = 0;
        char *cptr;
        char str[] = ".aif: audio/x-aiff";
        cptr = str;
        while(*cptr != ':')
          {
            printf("%c",str[i]);
            i++;
            cptr++;
          } 
 
        return 0;
 }

This can be modified for more accuracy but its just a suggestion

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.