Jeegar Patel
Jeegar Patel

Reputation: 27210

Is there any function available for printing some characters instead of whole strings?

see

char str[] = "hello world";
printf("%s",str);  

printf statement prints the all character in string before reaching '\0'

so what if i want to print just 4 1st character of str on stdout?

Upvotes: 1

Views: 153

Answers (7)

there is also a "substr()" function 

that return the substring from complete string.

example
printf("%s",substr(str,0,4));

it has syntax like this

substr(arrayName,charStartingPosition, lengthOfCharacters);

i hope this is easy to understand and no need to write more than 1 statement.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881383

For the first character, you can just use:

printf ("%c", *str);         // or
printf ("%c", *(str+0));     // or
printf ("%c", str[0]);

For a different character, just reach out and grab it by using an offset. For the second l at offset 3:

printf ("%c", str[3]);       // or
printf ("%c", *(str+3));

For a substring, you can use a combination of that method along with the maximum field width feature of printf:

printf ("%.2s", str+3);      // prints "lo"

With all these solutions, you want to make sure you don't start on the wrong side of the null terminator. That wouldn't be a good thing :-)

If you want a generalised solution that will work for any string, and is relatively safe in terms of finding the starting point, you can use:

void outSubstr (FILE *fh, char *str, size_t start, size_t sz, int padOut) {
    if (start >= strlen (str)) {
        if (padOut)
            fprintf (fh, "%*s", sz, "");
        return;
    }

    if (padOut)
        fprintf (fh, "%-*.*s", sz, sz, str + start);
    else
        fprintf (fh, "%-.*s", sz, str + start);
}

The parameters are as follows:

  • fh is the file handle to write to.
  • str is the start of the string.
  • start is the offset to start printing from.
  • sz is the maximum number of characters to print.
  • padOut is a flag indicating that sz is also the minimum size. Output will be padded with spaces on the right if there are not enough characters in the string to satisfy the size.

Upvotes: 2

Eregrith
Eregrith

Reputation: 4366

Really less painful for the system :

int main(void)
{
  char c;

  c = 'z';
  write(1, &c, 1);
}

No need for heavy stdio here

Then you can ...

char *s = "Hello, World!";
write(1, s, numberOfChars);

Or if you really want to do it char by char:

void printnchars(char *s, int n)
{
  int i;

  i = 0;
  while (i <= n)
  {
    write(1, s + i, 1);
    i++;
  }
}

Upvotes: 0

DonCallisto
DonCallisto

Reputation: 29912

numOfChars = 4;
printf("%.*s\n", numOfChars, "Hello, world!");

Where numOfChars is the quantity of characters that you want to print.

Upvotes: -1

Michael Chinen
Michael Chinen

Reputation: 18697

You can use %c in your format string:

printf("%c", *s);

prints 'H'

To print an arbitrary char:

printf("%c", s[3]);

prints 'l'

Upvotes: 2

Paul R
Paul R

Reputation: 212959

You can just specify the field width in the printf format string:

#include <stdio.h>

int main(void)
{
    const char *s = "Hello world !";

    printf("%.4s\n", s);
    return 0;
}

or, if you want to specify the field width at run-time:

#include <stdio.h>

int main(void)
{
    const char *s = "Hello world !";
    const int fw = 4;

    printf("%.*s\n", fw, s);
    return 0;
}

In either case the output will be:

Hell

Upvotes: 6

ext
ext

Reputation: 2903

This will print up to 4 characters.

printf("%.4s", str);

Upvotes: 1

Related Questions