C program to print n character to another string

Q. Print n character to another string.

SOLUTION :

Print Forward String :

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

int main() {
    int n;
    char* string[50];
    printf("\n Enter the string : ");
    gets(string);
    printf("\n Enter the value : ");
    scanf("%d",&n);

    printf("\n RESULT : \n");
    subs(string, n);
    return 0;
}

void subs(string, n){

  /* print at most first n characters (safely) */
  printf(" (%.*s)\n", n, string);

}

Print Reverse String :

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

int main() {
    int n;
    char* string[50];
    printf("\n Enter the string : ");
    gets(string);
    printf("\n Enter the value : ");
    scanf("%d",&n);

    printf("\n RESULT : \n");
    subs(string, n);
    return 0;
}

void subs(string, n){

  /* print last n characters (WARNING! Be careful with datatype conversion.) */
  printf(" (%s)\n", string + strlen(string) - n);

}

OUTPUT OF SOLUTION :

print n char of another string

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s