Program Discussion :: Strings
53 / 60
Write a program to enter the string then find the respective ascii value of the string.
Answer:
int myAtoi(char *str)
{
int res = 0; /*Initialize result*/
/* Iterate through all characters of input string and update result*/
for (int i = 0; str[i] != '\0'; ++i)
res = res*10 + str[i] - '0';
/* return result.*/
return res;
}
/*Driver program to test above function*/
int main()
{
char str[] = "89789";
int val = myAtoi(str);
cout
Asked In ::
Language:
Monika Jain
20 Jun, 2017 10:39 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Ascii_value() {
$str ="whst is your name"; /* input string */
echo "Input string is :" . $str."<br/>";
$strlength= strlen ($str) ;
echo "ASCII values for the string is :<br/>";
for( $x=0;$x< $strlength; $x++) /* loop for reading the input array */
{
$var= ord($str[$x]);
echo $str[$x]." =>". $var."<br/> ";
$var++;
}
}
Ascii_value() ;
?>
</body>
</html>
Language:
Lekha
7 Jul, 2017 9:30 AM
int myAtoi(char *str)
{
int res = 0; // Initialize result
// Iterate through all characters of input string and
// update result
for (int i = 0; str[i] != '\0'; ++i)
res = res*10 + str[i] - '0';
// return result.
return res;
}
// Driver program to test above function
int main()
{
char str[] = "89789";
int val = myAtoi(str);
printf ("%d ", val);
return 0;
}
Language:
Neel
7 Jul, 2017 9:30 AM
int myAtoi(char *str)
{
int res = 0; /*Initialize result*/
/* Iterate through all characters of input string and update result*/
for (int i = 0; str[i] != '\0'; ++i)
res = res*10 + str[i] - '0';
/* return result.*/
return res;
}
/*Driver program to test above function*/
int main()
{
char str[] = "89789";
int val = myAtoi(str);
cout<< val;
return 0;
}
Language:
Jaykumar
25 Aug, 2017 12:42 AM
import java.io.*;
import java.lang.*;
class CharToASCII
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the char");
String str=br.readLine();
for(int i=0; i<str.length(); ++i)
{
char c=str.charAt(i);
int j=(int) c;
System.out.println("ASCII OF "+c+"="+j+ ".");
}
}
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.Scanner;
public class ASCIIValue
{
public static void main(String []args)
{
String ascii="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string: ");
String str=sc.nextLine();
int len=str.length();
for(int i=0;i<len;i++)
{
char ch=str.charAt(i);
if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'))
{
ascii=ascii+(int)ch+" ";
}
}
System.out.println("ASCII values are: "+ascii);
}
}