Aaron7
Aaron7

Reputation: 277

Print (work with) substrings in a C program

How do I work with a string if I only want to print part of it?

For example, print "bcd" from "abcdef"?

I know how to work with a pointer to the beginning of a string, but I don't know how to determine the end.

void vypis(char *retezec)
{   
    printf("%s", retezec);
}

int main (void)
{
    char *str = NULL;
    size_t capacity;
        
    getline(&str, &capacity, stdin);    
    vypis(str);
    vypis(str+1);       
}

Upvotes: 0

Views: 166

Answers (4)

anastaciu
anastaciu

Reputation: 23802

I know how to work with a pointer to the beginning of a string, but I don't know how to determine the end.

A pointer to the last character you'd like to print is a possible solution:

void vypis(const char *retezec, const char *end)
{   
    while (retezec <= end && *retezec != '\0')
    {
        putchar(*retezec);
        retezec++;
    }
    putchar('\n');
}

int main (void)
{
    char *str = NULL;
    size_t capacity = 0;
        
    getline(&str, &capacity, stdin);    
    vypis(str, str + 5);      //prints from str[0] to str[5]
    vypis(str + 1, str + 3);  //prints from str[1] to str[3]     
}

Upvotes: 1

Captain Trojan
Captain Trojan

Reputation: 2921

You can temporarily set the first character you don't want to print out (e in your example) to \0.

void vypis(char *retezec, size_t delka){
    char aux = retezec[delka];
    retezec[delka] = '\0';
    printf("%s", retezec);
    retezec[delka] = aux;
}

To make sure it also works on string literals and other const char pointers:

void vypis(const char* str, size_t delka){
    char aux = retezec[delka];
    char* retezec = (char*) str;
    retezec[delka] = '\0';
    printf("%s", retezec);
    retezec[delka] = aux;
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

Here you are.

#include <stdio.h>

int main(void) 
{
    char *s = "abcdef";
    size_t pos = 1;
    int n = 3;
    
    printf( "%.*s\n", n, s + pos );
    
    return 0;
}

The program output is

bcd

Or another example

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

int main(void) 
{
    char *s = "abcdef";

    for ( int i = 0, n = ( int )strlen( s ); i < n; i++ )
    {
        printf( "%.*s\n", i + 1, s );
    }
    
    return 0;
}

The program output is

a
ab
abc
abcd
abcde
abcdef

You can write a generic function that can output a sub-string in any stream for example in an opened file.

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

FILE * print_substring( const char *s, size_t pos, size_t n, FILE * fp )
{
    size_t len = strlen( s );
    
    if ( pos < len )
    {
        if ( len - pos < n ) n = len - pos;
        
        fprintf( fp, "%.*s", ( int )n, s + pos );
    }
    
    return fp;
}

int main(void) 
{
    char *s = "abcdef";

    for ( int i = 0, n = ( int )strlen( s ); i < n; i++ )
    {
        putc( '\n', print_substring( s, 0, i + 1, stdout ) );
    }
    
    return 0;
}

The program output is

a
ab
abc
abcd
abcde
abcdef

Upvotes: 1

Olcay Ertaş
Olcay Ertaş

Reputation: 6228

You can try this:

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

void printSubString(const char *str, int start, int end) {
    int length = strlen(str);
    if (start >= length) {
        return;
    }
    if (end >= length) {
        return;
    }
    int index = start;
    while(index <= end) {
        printf("%c", str[index++]);
    }
    printf("\n");
}

int main() {
    char *str = "Hello, world!";
    printSubString(str, 3, 10);
    return 0;
}

Output:

lo, worl

Upvotes: 1

Related Questions