Print the ASCII value of character in a String

Problem Statement:- Program to Print the ASCII value of character in a String.

Sample Input/Output:-


Sample Input First:

csinfo

Sample Output First: 

c ==> 99
s ==> 115
i ==> 105
n ==> 110
f ==> 102
o ==> 111

Sample Input Second:

Java Program

Sample Output Second: 

J ==> 74
a ==> 97
v ==> 118
a ==> 97
  ==> 32
P ==> 80
r ==> 114
o ==> 111
g ==> 103
r ==> 114
a ==> 97
m ==> 109


Data requirement:-

   Input Data:- str

  Output Data:- str 

  Additional Data:-i, j

Program in C

Here is the source code of the C Program to Print the ASCII value of a character in a String.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[50];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i;
    printf("ASCII values of letters in string are:\n");
    for(i=0;i<strlen(str);i++)
    {
        printf("%c ==> %d\n",str[i],str[i]);
    }
}

Input/Output:
Enter your String:csinfo
ASCII values of letters in string are:
c ==> 99
s ==> 115
i ==> 105
n ==> 110
f ==> 102
o ==> 111

Program in C++

Here is the source code of the C++ Program to Print the ASCII value of character in a String.

Code:

#include<iostream>
using namespace std;
int main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int len=0,i;
    while(str[len]!='\0')
    {
        len++;
    }
    cout<<"ASCII values of letters in string are:\n";
    for(i=0;i<len;i++)
    {
        cout<<str[i]<<" ==> "<<int(str[i])<<"\n";
    }
}

Input/Output:
Enter your String:csinfo
ASCII values of letters in string are:
c ==> 99
s ==> 115
i ==> 105
n ==> 110
f ==> 102
o ==> 111

Program in Java


Here is the source code of the Java Program to Print the ASCII value of character in a String.

Code:

import java.util.Scanner;
public class p29 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
int i;
System.out.println("ASCII values of letters in string are:\n");
    for(i=0;i<str1.length();i++)
    {
    System.out.println(str1.charAt(i)+" ==> "+(int)(str1.charAt(i)));
    }
    cs.close();
}
}

Input/Output:
Enter your String:
Java Program
ASCII values of letters in string are:
J ==> 74
a ==> 97
v ==> 118
a ==> 97
  ==> 32
P ==> 80
r ==> 114
o ==> 111
g ==> 103
r ==> 114
a ==> 97
m ==> 109

Program in Python

Here is the source code of the Python Program to Print the ASCII value of character in a String.

Code:

str=input("Enter the String:")
print("ASCII values of letters in string are:")
for i in range(len(str)):
    print(str[i]," ==> ",(ord)(str[i]))

Input/Output:
Enter your String:
Java Program
ASCII values of letters in string are:

J ==> 74
a ==> 97
v ==> 118
a ==> 97
  ==> 32
P ==> 80
r ==> 114
o ==> 111
g ==> 103
r ==> 114
a ==> 97
m ==> 109

More:-

C/C++/Java/Python Practice Question 


Post a Comment

0 Comments