Thread: please, what is the best way to assign int's to a char array ?

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    122

    please, what is the best way to assign int's to a char array ?

    Code:
    char alphabet_string[CHAR_COUNT][26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\0' };
    Please, pseudo code, or is it possible inside a for loop, each letter of alphabet, 1 for a and 26 for z, inside a for loop, returning the int or a data type variable. Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    18
    each letter of alphabet, 1 for a and 26 for z, inside a for loop, returning the int or a data type variable.
    Can you clarify this, please? I can't understand exactly what you want to do.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by _jamie View Post
    Code:
    char alphabet_string[CHAR_COUNT][26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\0' };
    Please, pseudo code, or is it possible inside a for loop, each letter of alphabet, 1 for a and 26 for z, inside a for loop, returning the int or a data type variable. Thanks.
    It's not quite clear what you want to do. But say we want 1 to represent 'a' and 26 to represent 'z', and we want to write to a string at a position, we can write a function like this

    Code:
    void writeletter(char *str, int index, int letter)
    {
        assert(index >= 0 && index < strlen(str));
        assert(letter >= 1 && letter <= 26);
    
        str[index] = 'a' + letter -1;
    }
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    122
    looks good Malcom, I will study this code tonight. Great use of the use of assert() function, thanks.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    339

    Post

    each letter of alphabet, 1 for a and 26 for z, inside a for loop
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
    
    int main( int argc, char *argv[] ) {
      
        char userinput[256];
        
        printf( "enter string: " ); 
        fgets( userinput, 256, stdin ); 
        userinput[strlen(userinput)-1] = '\0';      
        
        for (int i=0; i<strlen(userinput); i++) {
          for (int j=0; j<strlen(alphabet); j++) {
            
            if (userinput[i] == alphabet[j]) {
              int converted = (j+1);
              printf( "%i,", converted );
            }
    
          }
        }
        
      printf( "\n" );
      return 0;
    }
    Last edited by Structure; 06-16-2021 at 09:30 AM.
    "without goto we would be wtf'd"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    > char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
    There is no guarantee that you're getting a \0 in your string.
    So strlen() on it is broken.
    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.

  7. #7
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    339
    Quote Originally Posted by Salem View Post
    > char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
    There is no guarantee that you're getting a \0 in your string.
    So strlen() on it is broken.
    Code:
    char alphabet[27] = "abcdefghijklmnopqrstuvwxyz\0";
    "without goto we would be wtf'd"

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    Make the compiler do the work.
    Code:
    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    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.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,908
    >looks good Malcom, I will study this code tonight. Great use of the use of assert() function, thanks.

    It's all fun and games until NDEBUG is defined :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i assign values to ponter char array????
    By De shaa in forum C Programming
    Replies: 2
    Last Post: 08-11-2013, 04:01 AM
  2. Assign part of char array to vector
    By Ducky in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2013, 12:01 PM
  3. how do I assign a char array to an int array?
    By mark 555 in forum C Programming
    Replies: 7
    Last Post: 12-13-2012, 10:24 PM
  4. Replies: 3
    Last Post: 10-03-2012, 01:23 AM
  5. Assign value of multiple char array elements to int
    By 3saul in forum C Programming
    Replies: 3
    Last Post: 02-03-2006, 05:31 AM

Tags for this Thread