codescracker
c

C Program to Print Next Successive Character & Adjacent Character



« C Tutorial C Examples »

In this tutorial, we will learn about how to create a program in C that will print next successive character and adjacent character. Here first we have created a program that will ask from user to enter any character to find and print its next successive character. Also we have created a program that will take a character as input (from user at run-time) and then will print its adjacent character.

C Print Next Successive Character

Let's first create a program that will print next successive character of given character by user. Let's suppose that if user has entered a as character input, then program will print b (the next successive character of a). Or if user will provide C as character input, then program will print D as output.

The question is, write a program in C, to accept a character from user and process it in these two ways:

  1. If character is an alphabet then print its next successive character. For example, if user enters a then print b. If x then print y, and if z then print a.
  2. If it is not an alphabet then print the character itself as output

The answer to this question is given below:

// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    printf("Enter any character: ");
    scanf("%c", &ch);
    printf("\n");
    if(ch>=65 && ch<90)
        printf("%c", ch+1);
    else if(ch>=97 && ch<122)
        printf("%c", ch+1);
    else if(ch==90)
        printf("%c", 65);
    else if(ch==122)
        printf("%c", 122);
    else
        printf("%c", ch);
    getch();
    return 0;
}

As the program given above was written under the Code::Blocks IDE, therefore after successful build and run, here is the sample run:

c program print next character

Now supply any character as input say a and press ENTER key to see the next character that comes after a. Here is the second snapshot of the sample run:

print next character c program

Let's check the same program with another sample run. This time provide the input as Z and press ENTER key:

print next successive character c

Here is another sample run that checks what will happen if user supplies any input that is not an alphabet character:

c print next character program

Program Explained

C Print Adjacent Character

Let's create another program that will print adjacent character of any given character by user at run-time. Here adjacent characters are the two character, one comes before and second comes after the given character. For example, if user has entered A, then print its adjacent character as Z and B. Or if user has entered D, then print C and E

// ----codescracker.com----

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    printf("Enter any character: ");
    scanf("%c", &ch);
    printf("\n");
    if(ch>65 && ch<90)
        printf("Adjacent characters = %c and %c", ch-1, ch+1);
    else if(ch>97 && ch<122)
        printf("Adjacent characters = %c and %c", ch-1, ch+1);
    else if(ch==90)
        printf("Adjacent characters = %c and %c", ch-1, 65);
    else if(ch==122)
        printf("Adjacent characters = %c and %c", ch-1, 97);
    else if(ch==65)
        printf("Adjacent characters = %c and %c", 90, ch+1);
    else if(ch==97)
        printf("Adjacent characters = %c and %c", 122, ch+1);
    else
        printf("%c", ch);
    getch();
    return 0;
}

Here is the final snapshot of sample run:

c print adjacent characters

The concept used in above program is similar as used in previous program. Except that we have to print the previous character along with next character. Therefore we have used ASCII code for A, a, Z, and z in separate cases.


« C Tutorial C Examples »