In: Computer Science
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue).
Sample Output
Please enter your FULL name
Billy Joe McCallister
Here is the name Billy Joe McCallister in reverse:
retsillaCcM eoJ ylliB
Billy Joe McCallister consists of 21 characters
Code:
#include<stdio.h>
#include<string.h>
int userName(char str[])
//userName() function
{
int i,count=0;
//iterative variable i and count variable to count number of
characters
printf("Here is the name %s in
reverse:\n",str);
for (i=strlen(str)-1;i>=0;i--)
//iterating loop from last character to starting
character by calculating string length .
{
printf("%c",str[i]);
//printing each character
from the end
count++;
//incrementing the count it
is nothing but counting the number of characters
}
printf("\n");
return count;
//Finally returning the count value
}
int main()
{
char str[100];
printf("Please enter the Full Name\n");
//asking user to enter the
full name
gets(str);
//taking input from user
int num_of_characters=userName(str);
//calling
userName() function by passing str paramter and storing num of
characters returned by it in variable
printf("%s consists of %d
characters\n",str,num_of_characters);
//printin the number of characters the string
consists of
return 0;
}
Code and Output Screenshots:
Note: if you have any queries please post a comment thanks a lot... always available to help you..