Write a C program to print the first and last character of a string.
#include <stdio.h>
#include <string.h>
int main() {
char string[256];
/* get the input string from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* printing first character in input string */
printf("First Character in the given input string:");
printf("%c\n", string[0]);
/* printing last character in input string */
printf("Last Character in the given input string:");
printf("%c\n", string[strlen(string) - 1]);
return 0;
}
#include <string.h>
int main() {
char string[256];
/* get the input string from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* printing first character in input string */
printf("First Character in the given input string:");
printf("%c\n", string[0]);
/* printing last character in input string */
printf("Last Character in the given input string:");
printf("%c\n", string[strlen(string) - 1]);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:Hello World
First Character in the given input string: H
Last Character in the given input string : d
Enter your input string:Hello World
First Character in the given input string: H
Last Character in the given input string : d
No comments:
Post a Comment