Like Tree2Likes
  • 2 Post By Salem

how to increment chars while skipping the first few

This is a discussion on how to increment chars while skipping the first few within the C Programming forums, part of the General Programming Boards category; lets say that I want a char array that reads in chars but I want the first nine reserved for ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    150

    how to increment chars while skipping the first few

    lets say that I want a char array that reads in chars but I want the first nine reserved for something else and I want first there to be one set of chars like input 13 output char 10 and I want to increment the chars when dealing with more than one while skipping the first nine like 14,14 = 11,11
    how can I do this?
    Last edited by Once-ler; 02-21-2013 at 04:34 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I'm pretty sure that I don't understand your problem but if you want to reserve the first nine characters of the array you could do something like:
    Code:
    #include <stdio.h>
    #include <string.h>
     
    #define SIZE 20
    
    int main(void)
    {
        char arr[SIZE];
        strcpy(arr, "reserved!");
        puts("Enter some text:");
        fgets(&arr[9], SIZE - 9, stdin);
        puts("\ncontents of arr:");
        puts(arr);
    
        return 0;
    }
    This will put 9 characters into the array, then reads from stdin and stores the input starting at the 10th position of the array.

    Is this close?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Thanks but not close
    I guess I will have to read more books before I will be able figure this out if I ever do
    I mean something like a=j and a different one ab=jk and abd = jkm
    Last edited by Once-ler; 02-21-2013 at 06:00 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Quote Originally Posted by Once-ler View Post
    Thanks but not close
    Well there's a dis-incentive to bother helping you again.

    How about posting some written examples of the kind of thing you're trying to do.

    One vague description then a reply of "nope, not it" is not the way to get progress in any walk of life.
    Crossfire and Alexis like this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I was just telling him that it was not what I am looking for, I thanked him and was not rude

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    well then how about being more descriptive, and telling us, or better yet showing us what it is you want, instead of just "not it"

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I am trying to show what I mean
    j=a jk=ab jkmnop=abd a program that goes through chars and changes them to different chars sometimes with a few digits sometimes with a lot, I need some way of incrementing the correct number of digits

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Sorry, but that looks like you fell asleep on the keyboard to me.

    Wild guess time
    You have
    aaaaa
    then you want
    aaaab
    until it gets to
    aaaaz
    and then you want
    aaaba
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Once-ler View Post
    j=a jk=ab jkmnop=abd a program that goes through chars and changes them to different chars sometimes with a few digits sometimes with a lot, I need some way of incrementing the correct number of digits
    If you don't tell us the general rules (which characters should be incremented by which amounts), we can only guess.

    How about using a translation table/array, where you store for each character the new character:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int translate[256] = {[106] = 97, 98, 99, 100};  // works only in C99 and later
        char *test = "jkmnop";
    
        printf("%s = ", test);
        for (int i = 0; test[i] != '\0'; i++)
            putchar(translate[(unsigned char) test[i]]);
    
        putchar('\n');
        return 0;
    }
    The idea is to use the character as index for the translation array. For example if the character is 'j' (decimal 106 in ASCII), translate['j'] == translate[106] == 97 == 'a'.
    You have to do this for every character you want to translate.

    Bye, Andreas

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,500
    Is what you want something like this:
    Code:
    /* Start at ninth character */
    int i = 9;
    int ch;
    
    /* Put input into String starting from the ninth character */
    while ( (ch = getchar()) != '\n' && ch != EOF)
    {
      String[i] = ch;
      i++;
    }
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  2. Replies: 4
    Last Post: 05-26-2011, 07:51 AM
  3. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 11:42 AM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 04:03 AM
  5. Taking and skipping chars from files
    By Korhedron in forum C++ Programming
    Replies: 7
    Last Post: 07-23-2003, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21