Home > Mobile >  How to print a certain character of a string in a certain position
How to print a certain character of a string in a certain position

Time:11-23

Is there a way to print a specified character of a string in a certain position?

For example:

char str[]="Hello";

Output:
    o

I have to print the letter "o" in the position indicated by its index (in this case "4");

CodePudding user response:

You can replace 4 below with any number from 0 to 4.

    char kth = *(str   4);
    printf("\n%c", kth);

output:

o

CodePudding user response:

This will print out the nth character from the string, padded on the left and right with spaces

void print_nth_padded(const char *str, size_t n) {
    for (size_t i = 0; i < n; i  ) 
        puts(" ");
    printf("%c", str[n]);
    for (size_t i = n   1; i < strlen(str); i  )
        puts(" ");
}

So for example, print_nth_padded("Hello", 4); will print out 4 spaces, followed by o.

CodePudding user response:

I hope this code can help. It simply verifies if the given index makes sense (if it is in the bounds of the array), and prints spaces befor it prints the actual character.

    #include <stdio.h>
    #include <string.h>
    
    void print_char(const char * string, size_t index)
    {
        if (index >= strlen(string))
        {
            printf("The index is not correct\n");
            return;
        }
        size_t i = 0;
        while (i < index)
        {
            printf(" ");
            i  ;
        }
        printf("%c\n",string[i]);
    }

Note: calling printf this many times is not optimized, but I thought to leave the code like that for clarity. A more optimised version would first construct the output array of characters and once ready print it.

So we could use malloc for this need:

    void print_char(const char * string, size_t index)
    {
        size_t buffer_size = strlen(string);
        if (index >= strlen(string))
        {
            printf("The index is not correct\n");
            return;
        }
        char * buf = malloc(sizeof(char )* (buffer_size 1));
        size_t i = 0;
        while (i < index)
        {
            buf[i] = ' ';
            i  ;
        }
        buf[i] = string[i];
        buf[i 1] = '\0';
        //At this point your buffer is constructed
        printf("%s\n",buf);
        free(buf);
    }

CodePudding user response:

You can have a function that prints the whitespace character index times. When index is reached, the character in that position is displayed.

void display(const char* str, const int index)
{
    for (size_t i = 0; i < index; i  )
        putc(' ', stdout);

    putc(str[index], stdout);
}

Note that it is your responsibility to verify index does not cross the size of the character array. For example,

const char str[] = "Hello";
size_t str_len = sizeof(str) / sizeof(str[0]);

const int index = 7;

if (index >= str_len || index <= 0) {
    fprintf(stderr, "error: Attempted to access out-of-bound!\n");
    return EXIT_FAILURE;
}

If we consider the full version of the program, we have:

#include <stdio.h>

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

void display(const char* str, const int index)
{
    for (size_t i = 0; i < index; i  )
        putc(' ', stdout);

    putc(str[index], stdout);
}

int main(void)
{
    const char str[] = "Hello";
    size_t str_len = sizeof str / sizeof str[0];

    const int index = 4;

    // Verification of the validity of the array index.
    if (index >= str_len || index <= 0) {
        fprintf(stderr, "error: Attempted to access out-of-bound!\n");
        return EXIT_FAILURE;
    }

    // Calling the function safely.
    display(str, index);

    return EXIT_SUCCESS;
}

Now, we have the following output:

$ gcc -std=c99 -g 1.c && ./a.out
    o

CodePudding user response:

Use the specified character to find its position in the string.

char str[]="Hello";
char specified_character = 'o';
char *position = strchr(str, specified_character);

If found, print the character with left side ' ' padding by passing in a width.

if (position) {
  int padding = position - str;
  int width = padding   1;
  printf("%*c\n", width, str[padding]);
}

Output: o

  •  Tags:  
  • c
  • Related