#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    26
    Rep Power
    0

    Convert string to array


    Hope someone can identify how to change the following code, which will only print the first 10 chars of an entered string, no matter how many chars are entered. I understand that my while-loop is improperly constructed, but what correction do I need in order that as many characters that are printed are affected by scanf()?
    Code:

    #include <stdio.h>
    #include<string.h>
    #define SIZE 125
    /*Make a char array and print it backwards*/

    int main(void)
    {
    int i, total;
    char arr[SIZE];

    i = 0;
    printf("Enter text to print backwards:\n");

    while(i!='\n')
    {
    scanf("%c", &arr[i]);
    i++;
    }
    //evaluate strlen() to print
    total = strlen(arr);
    printf("\n");
    //print array elements backwards
    for(i = 0;i<total;i++)
    //for(i = total;i>=0;i--)
    {
    printf("%c",arr[i]);
    }
    printf("\n");

    return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    8
    This is your 14th post on this forum and you still don't know how to use CODE tags and indentations? The code is not even yours. Laziness at max level.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Code:
       //evaluate strlen() to print
         total = strlen(arr);
            printf("\n");
             //print array elements backwards
        for(i = 0;i<10;i++)
        {
        
    if(i>=total) printf(" ");
    else printf("%c",arr[9-i]);
    } printf("\n");
    Matt
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    26
    Rep Power
    0
    Originally Posted by travellingmatt
    Code:
       //evaluate strlen() to print
         total = strlen(arr);
            printf("\n");
             //print array elements backwards
        for(i = 0;i<10;i++)
        {
        
    if(i>=total) printf(" ");
    else printf("%c",arr[9-i]);
    } printf("\n");
    Matt
    Thanks;
    Problem was with scanf portion of code, which I finally solved.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    26
    Rep Power
    0
    Originally Posted by DRK82
    This is your 14th post on this forum and you still don't know how to use CODE tags and indentations? The code is not even yours. Laziness at max level.
    Sorry to distress you so, but I'm just trying to learn & understand C programming, & thought you might help. Didn't pay attention to perfect style in submitting my question. Will do better next time.
    ----and why do you think the "code is not even yours"? Who else would write such imperfect code?

IMN logo majestic logo threadwatch logo seochat tools logo