Thread: Program to turn a string into ASCII code

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    81

    Program to turn a string into ASCII code

    Code:
    #include<stdio.h>
    
    int main(){
      
        char str[5000];
        int i=0;
    
    
        printf("Enter any string: ");
        scanf("%s",str);
    
    
        printf("ASCII values of each characters of given string: ");
        while(str[i])
             printf("%d ",str[i++]);
            
       
        return 0;
    }
    Hi, this works fine except that it only outputs ASCII code as far as the first word (so if its a sentence the user inputs then it will only go as far as the end of the first word).

    What have I done wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    %s stops at white space.

    If you want to read a whole like, use fgets()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    81
    That's great. Thanks a million!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn C CMD program into a windowed app
    By spadez in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 03-01-2009, 03:07 PM
  2. Why does the loop in my program turn infinite?
    By DaniiChris in forum C Programming
    Replies: 6
    Last Post: 07-08-2008, 02:44 AM
  3. How can I turn this code into a case switch statement?
    By Lonck in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2007, 09:14 AM
  4. Turn a string into an array
    By The Letter J in forum C++ Programming
    Replies: 8
    Last Post: 12-22-2004, 12:08 PM
  5. how to turn this into c++ code.
    By InvariantLoop in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2004, 01:48 PM