Thread: Convert to ASCII program almost working

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    22

    Convert to ASCII program almost working

    I am programming a program that will convert a string (char) into it's ASCII value, I am having one issue though. The output is correct but I get a value of " 10 " at the end of the output every single time I run the program ? Any thoughts on why this might be ? I am newer to C , for what it is worth. Thank you all in advance for any suggestions!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    const int size = 50;
    
    
    int main()
    {
        char word[size];
        int x = 0;
        
        
        
        printf ("Please enter a line of text ( Max 25 characters) or 'Q' or 'q' to quit:\n\n");
           
        fgets(word, size, stdin);
    
        while (word[x] != '\0')    
        {
            printf ("The ASCII for this word is:  %d%\n\n",word[x]);    
            x++;
        }
       for( x = 0; x < 1; x++)
            printf("%c", toupper(word[0]));
            printf("%c", tolower(word[1]));
            printf("%c", toupper(word[2]));
            printf("%c", tolower(word[3]));
            printf("%c", toupper(word[4]));
            printf("%c", tolower(word[5]));
            printf("%c", toupper(word[6]));
            printf("%c", tolower(word[7]));
            printf("%c", toupper(word[8]));
            printf("%c", tolower(word[9]));
            printf("%c", toupper(word[10]));
            printf("%c", tolower(word[11]));
            printf("%c", toupper(word[12]));
            printf("%c", tolower(word[13]));
            printf("%c", toupper(word[14]));
            printf("%c", tolower(word[15]));
            printf("%c", toupper(word[16]));
            printf("%c", tolower(word[17]));
            printf("%c", toupper(word[18]));
            printf("%c", tolower(word[19]));
            printf("%c", toupper(word[20]));
            printf("%c", tolower(word[21]));
            printf("%c", toupper(word[22]));
            printf("%c", tolower(word[23]));
            printf("%c", toupper(word[24]));
            printf("%c", tolower(word[25]));
            
            
             
    
        getchar();
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    What do you mean? what has a value of 10?

    BTW, you need to read up on for loops. That loop there has no purpose, as it always repeats "printf("&#37;c", toupper(word[0]));" exactly once.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    10 is the ascii value for a newline '\n'

    When you hit enter a newline is appended to the buffer.

    fgets then appends a null to terminate the string.

    You are processing the newline as part of the buffer, which it is.
    Last edited by rmetcalf; 09-16-2008 at 12:17 PM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    Try this:

    Code:
    	char input[51];
    	int i;
    	
    	printf("Enter something...\n");
    	scanf("&#37;[^\n]", input);
    	
    	
    	printf("\n"); // newline
    	
    	for(i = 0; i < strlen(input); i++)
    		printf(" %c = %d\n", input[i], input[i]);
    		
    	printf("\n\nPress any key to continue...");
    	getch();
    My output was:

    Code:
    Enter something...
    sentence
    
     s = 115
     e = 101
     n = 110
     t = 116
     e = 101
     n = 110
     c = 99
     e = 101
    
    Press any key to continue...
    Also, your other piece of code needs to be modified.

    Code:
    for(x = 0; x < strlen(word); x++)
    {
            word[x] = toupper(word[x]);
            printf("%c", word[x]);
    }
    Last edited by george_1988; 09-16-2008 at 06:22 PM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    Thank you !

    Actually on that second part , I am trying to have the string alternate case , so ..

    user inputs : dog

    the out put would be : DoG

    I cant seem to get it to work in a loop though ? I have tried something to the effect

    Code:
    char c = 'A';
    char lower = tolower(c);
    char upper = toupper(lower);
    Any suggestions ? As you can see , my first attempt was gut to read and apply the tolower or toupper to the array index but if I dont fill the array I get a bunch of output that is junk.

    ok, thank you for any suggestions !

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    ggraz,

    To achieve that you will need to use two loops. Look at the following code:

    Code:
    #include <stdio.h>
    #include <ctype.h>  /* for toupper() & tolower() */
    #include <string.h> /* for strlen() */
    
    int main(void)
    {
    	char input[51];
    	int i;
    	
    	printf("Enter something...\n");
    	scanf("&#37;[^\n]", input);
    	
         /* convert every character to uppercase first */
    
         for(i = 1; i < strlen(input); i++)
              input[i] = toupper(input[i]);
              
         /* convert every other character to lowercase */
    
         for(i = 1; i < strlen(input); i++)
         	input[i*2] = tolower(input[i*2]);
    
    	printf("%s", input);
    	
    	getch(); /* to view output */
    }
    My output:

    Code:
    Enter something...
    sentence
    sEnTeNcE
    It is also worth noting if you add spaces in your string, your upper, lower, upper, lower, will be redundant as the space will be read as a character and will get converted to upper/lower.

    For example, see the following output:

    Code:
    Enter something...
    sentence
    sEnTeNcE TeSt
    Note the two capital letters in a row before and after the space. See if you can correct this issue. You might want to use the isspace() function.
    Last edited by george_1988; 09-17-2008 at 06:06 PM.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    ok, I see my error ! Thank you so much ! I was trying to do it with only one loop ! thus the program was giving me fits ! Always learning ! Thank you !

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ummmm if I understand your objective correctly, why not just do this:

    Example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define BUFFERSIZE 256 // .25k is sufficient
    
    int main(void)
    {
      char buffer[BUFFERSIZE] = {0}, *p;
    
      do
      {
        fgets(buffer, sizeof(buffer), stdin);
        strtok(buffer, "\n");
    
        puts("HEX    DEC\n___    ___\n");
        for(p = buffer;*p;++p)
           printf("&#37;#x%4d\n, *p, *p); 
      } while(*buffer);
    
      return 0;
    }
    Just throw in a !isspace() in there if you are trying to go word by word or whatever.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by ggraz View Post
    ok, I see my error ! Thank you so much ! I was trying to do it with only one loop ! thus the program was giving me fits ! Always learning ! Thank you !
    You can do it with one loop too:
    Code:
    for(int i=0;i<size;++i)
      if(i%2)
        putchar( toupper(word[i]) );
      else
        putchar( tolower(word[i]) );
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not working right
    By blindman858 in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2005, 09:37 AM
  2. Program not working
    By jat421 in forum C Programming
    Replies: 6
    Last Post: 03-20-2005, 08:28 PM
  3. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM
  4. How To Convert A Dos-Prompt C++ PRogram Into Win32 Application ?
    By SonicWave in forum Windows Programming
    Replies: 1
    Last Post: 09-15-2001, 11:03 AM
  5. Can anybody convert this program.
    By andy bee in forum C Programming
    Replies: 3
    Last Post: 08-28-2001, 05:07 PM