Strings and Characters in arrays.

This is a discussion on Strings and Characters in arrays. within the C Programming forums, part of the General Programming Boards category; Hi, I am pretty new to C and it would be so great if I could get some help asap. ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Question Strings and Characters in arrays.

    Hi,

    I am pretty new to C and it would be so great if I could get some help asap.

    I am currently trying to read a string from user input, so I have:

    printf("please enter your name\n");
    scanf ("%s", &name);

    But here's the part i'm lost at:
    so say the user enters their name 'jane'
    I would like to capture each letter separately as characters, in an array, so I can print it out like
    j
    a
    n
    e
    I would also like to then know how I assign a letter a number, like a-z is 1-26. So that jane could alternatively be printed out as
    10, 1, 14, 5

    ive tried everything I can think of. Please help!

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    scanf ("%s", name);

    that should solve your problem

    and array can be passed as an address

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    take the & away

    I already had

    scanf("%s", &name); for getting the input string.

    So does taking away the & help? I still need to know how to get each letter captured as a single character, so i can do a for loop and print it like
    j
    a
    n
    e.

    I am not great at explaining, sorry

    like i tried this:

    Code:
    int main () {
    
    char* name[30];
    int i;
    int length;
    
    printf("please enter your name\n");
    scanf ("%s", &name);
    length = strlen(plaintext);
    for(i=0;i<length;i++){
    printf ("%c", plaintext[i]);  }
    
    }
    but it doesnt work, it outputs gobbledegook.

    ty

  4. #4
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Quote Originally Posted by akidamo
    I already had

    scanf("%s", &name); for getting the input string.

    So does taking away the & help? I still need to know how to get each letter captured as a single character, so i can do a for loop and print it like
    j
    a
    n
    e.

    I am not great at explaining, sorry

    like i tried this:

    Code:
    int main () {
    
    char* name[30];
    int i;
    int length;
    
    printf("please enter your name\n");
    scanf ("%s", &name);
    length = strlen(plaintext);
    for(i=0;i<length;i++){
    printf ("%c", plaintext[i]);  }
    
    }
    but it doesnt work, it outputs gobbledegook.

    ty
    char* is a pointer not an array, char* name[30] is an array of pointers if you want to use a pointer then allocate memory for the pointer to point to so you can put data into the allocated memory, or use a character array.
    plaintext, I don't see this variable decalred?
    and lose the & like was already said previously.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Ok

    ok yea, sorry plaintext was supposed to be name, i just changed it for the post, forgot to change that. Ok thanks, like i said im really new to this and the rules, ill try what you've suggested, thanks.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    yea thanks it works so easy

    I was being silly with the pointers thingy, I had been trying to teach myself C from online documentation, and I got mixed up.

    But easy when u know how

    Just one more question though.... to assign the letters a-z a numerical value, is there a quick way of doing this?

    So I can have

    10
    1
    14
    5

    as alternative output, when a user inputs a string?

    Thanks

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If you can assume ASCII or some other character set that guarantees that alphabetic characters are contiguous, then you could do something like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main()
    {
       char line[80];
       fputs("text? ", stdout);
       fflush(stdout);
       if ( fgets(line, sizeof line, stdin) != NULL )
       {
          int i;
          char *newline = strchr(line, '\n');
          if ( newline != NULL )
          {
             *newline = '\0';
          }
          for ( i = 0; line[i] != '\0'; ++i )
          {
             printf("%c %d\n", line[i], toupper(line[i]) - 'A' + 1);
          }
       }
       return 0;
    }
    
    /* my output
    text? Jane
    J 10
    a 1
    n 14
    e 5
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Many thanks, thats been great help

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    Hi again,

    sorry another silly question,
    I now want to do a mathematical function, so I can put an int to its power of 3, then modulus 33.
    eg: num2 = num1(to power of 3) % 33
    But I dont know how to tell the program to put the int to Power of 3. Can you help?

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Easy:
    Multiply num1 3 times

    Not as easy:
    include math.h and use the pow() function
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    36
    thanks yea ill have to use pow() then, cus its not always necessarily to power of 3, thanks

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Question sorry another prob

    another problem sigh....with a different program

    could I please again have some help?

    I want the program to read in a list of integers to sort, from the user.
    I then want to split this list in half. Say the list has 6 integers. I want to put the first 3 into a new array, and the last three into a third array.
    I can get the first 3 in a second array by doing this:

    Code:
    split = n/2; 
    
    		   for (i = 0; i < split; i++) {
    			  ab[i] = a[i];
    		printf ("this is in ab %d\n", ab[i]);
    		 Odd_even_sort(ab, split);     }
    	 }
    which is probably not very efficient, but anyway i am having trouble figuring out how to get the last three in the third array.

    Me doh, sorry.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    half = totalcount / 2;
    for( x = 0; x < half; x++ )
        newarray1[ x ] = oldarray[ x ]; /* copy the first half */
    
    for( y = 0, x = half; y < half; y++, x++ )
        newarray2[ y ] = oldarray[ x ]; /* copy the second half */
    Or, we could abuse the way the first for loop works with x, and do this:
    Code:
    for( x = 0; x < half; x++ )
        newarray1[ x ] = oldarray[ x ];
    
    for( y = 0; y < half; y++, x++ )
        newarray2[ y ] = oldarray[ x ];
    We could further abuse x and y, but I'll leave that up to you.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    36

    Unhappy trickier...

    Ok yesterday I was helped converting a letter to a number, like a to 1, b to 2, c to 3 etc... with the code in a previous post.

    So I modified that to work in my own program.
    but now I have the opposite problem, I would like to read in a small string of numbers, and convert them back to the letters.

    say a person enters "10 1 5 6", ...then the computer would output jane.

    Ive tried but failing miserably, theres a few problems i can think of:
    getting the whole string, and ignoring the spaces.
    getting the numbers separate,
    damn i'm a bit lost where I start,
    could anyone point me in the right direction.
    Sorry!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 08:53 AM
  2. Strings and Substrings using arrays
    By dcwang3 in forum C Programming
    Replies: 12
    Last Post: 02-18-2008, 06:28 AM
  3. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 02:13 PM
  4. strings or arrays of characters?
    By Callith in forum C++ Programming
    Replies: 13
    Last Post: 12-26-2004, 10:28 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

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