Scanning ASCII value of each character of a string

46,069

Solution 1

yeah it's very easy ..just a demo

int main()
{
 char *s="hello";
 while(*s!='\0')
  {
  printf("%c --> %d\n",*s,*s);
  s++;
  }
 return 0;
}

But make sure your machine is supporting the ASCII value format. In C every char has one integral value associted with it called ASCII. Using %d format specifier you can directly print the ASCII of any char as above.

NOTE: It's better to get good book and practice this kind of program yourself.

Solution 2

Given a string in C:

char s[] = "john";

or in C++:

std::string s = "john";

s[0] gives the numeric value of the first character, s[1] the second an so on.

If your computer uses an ASCII representation of characters (which it does, unless it's something very unusual), then these values are the ASCII codes. You can display these values numerically:

printf("%d", s[0]);                     // in C
std::cout << static_cast<int>(s[0]);    // in C++

Being an integer type (char), you can also assign these values to variables and perform arithmetic on them, if that's what you want.

I'm not quite sure what you mean by "scan". If you're asking how to iterate over the string to process each character in turn, then in C it's:

for (char const * p = s; *p; ++p) {
    // Do something with the character value *p
}

and in (modern) C++:

for (char c : s) {
    // Do something with the character value c
}

If you're asking how to read the string as a line of input from the terminal, then in C it's

char s[SOME_SIZE_YOU_HOPE_IS_LARGE_ENOUGH];
fgets(s, sizeof s, stdin);

and in C++ it's

std::string s;
std::cin >> s;  // if you want a single word
std::getline(std::cin, s); // if you want a whole line

If you mean something else by "scan", then please clarify.

Solution 3

You can simply get the ascii value of a char by casting it to type int:

char c = 'b';
int i = c; //i contains ascii value of char 'b'

Thus, in your example the code to get the ascii values of a string would look something like this:

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main()
{
    string text = "John";

    for (int i = 0; i < text.size(); i++)
    {
        cout << (int)text[i] << endl; //prints corresponding ascii values (one per line)
    }
}

To get the corresponding char from an integer representing an entry in the ascii table, you just have to cast the int back to char again:

char c = (char)74 // c contains 'J'

The code given above was written in C++ but it basically works the same way in C (and many other languages as well I guess)

Solution 4

There is no way to turn a string of length 'x' into x variables. In C or C++ you can only declare a fixed number of variables. But probably you don't need to do what you are saying. Perhaps you just need an array, or most likely you just need a better way to solve whatever problem you are trying to solve. If you explain what the problem is in the first place, then I'm sure a better way can be explained.

Solution 5

Ya,I think there are some more better solutions are also available but this one also be helpful. In C

#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(){
  char s[]="abc";
  int cnt=0;
  while(1){
    if(s[cnt++]==NULL)break;
  }
  int *a=(int *)malloc(sizeof(int)*cnt);
  for(int i=0;i<cnt;i++)a[i]=s[i];
  for(int i=0;i<cnt-1;i++)printf("%d\n",a[i]);  
  return 0;
}

In C++

#include <iostream>
#include <string>
using namespace std;
int main(){
    string s="abc";
    //int *a=new int[s.length()];
    //for(int i=0;i<s.length();i++)a[i]=s[i];
    for(int i=0;i<s.length();i++)
    cout<<(int)s[i]<<endl;
    return 0;
}

I hope this one will be helpful..

Share:
46,069
Kamal Kafkaesque
Author by

Kamal Kafkaesque

Pursuing Computer Engineering from India. Have lots of Interest in C programming Language. Just want to gain more and more knowledge about the Programming..

Updated on February 03, 2020

Comments

  • Kamal Kafkaesque
    Kamal Kafkaesque about 4 years

    Is there anyway , if I enter any string , then I want to scan ASCII value of each character inside that string , if I enter "john" then I should get 4 variables getting ASCII value of each character, in C or C++