INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Split string into individual characters

Split string into individual characters

(OP)
Hi

I'm trying to split a string of variable length into its individual component characters and am stuck.

For example 1234de would be given as

1
2
3
4
d
e

Any guidance would be gratefully received!

Thanks!

RE: Split string into individual characters

2
in the future, when asking for help on homework, you should post what has you stuck and/or what you have already tried or so we can point out where your errors are. Just giving someone the code to turn in teaches nothing, and the student learns nothing.

Simple C strings are just arrays of chars with a null char at the end of it. There are two ways to iterate through each character in the string; non-pointer and pointer version. I will go through both as I am not sure how your instructor wants it done.

CODE

// this is the beginner's only (Non-Pointer) way.
int i=0;
char P=str[i++];  // P= first char and increment I
  while (P)         //tests for null char;  null = false
  {
    //do what you want with that char
    printf("%c\n", P);
    P=str[i++];     // P = next char and increment i
  };
//--------------------------------------------------------
// this is a more preferred method 
char *P=str;
while(*P)
{
  // do what ever with current char; remember to de-reference the pointer to get the char (*P)
  printf("%c\n", *P);
  P++;  // get next char;
}; 

RE: Split string into individual characters

(OP)
Many thanks indeed for your help!

Not homework, I assure you! I am a 52 year old, long out of the world of school/college, simply seeking advice on the preferred method.

Best regards

Graham Laming

RE: Split string into individual characters

I apologize then; as this is a very common excercise in beginnning C classes, I hope you can understand my response. My second example is a more preferred way of traversing a null terminated string, mainly because you don't have the extra index maintence code being generated. However, if you are going to need the string's length somewhere in your code, then you may want to use the first method, as i will equal the number of chars in str when the loop is done (minus the null terminator) .

RE: Split string into individual characters

As an aside note; in my MIPS little-endian + XBurst SIMD set disassembler I'm currently working on, I have used a code *very* similar to "example A" above for manipulating strings without the use of the <strings.h> header (would make things easier, but I hate the bloat of all those unused functions & definitions in my finished executables).

Also, if you didn't notice it in the examples Prattaratt gave; in C, a string variable is actually a POINTER to an array of characters already! The only thing that differentiates a string from a char[] array, in practice, is that a string must always end with a '\0' character; while a char or a char[] array doesn't have to. Thus, for your reference string above:

CODE

#include <stdio.h>

int main(){
   char *string; // declare a pointer of type char to reference our string
   int x; // integer variable used for traversing, and measuring the length of, our string

   string="1234de"; // stores the MEMORY ADDRESS of the first character in the string "1234de" in variable "string"
   x=0;

   while(string[x]){
      printf("Char #%d=%c\n", x, string[x]);
      x++; // IMPORTANT, if you don't increment x BEFORE exiting the loop, then the final count in x will be 1 less than
   }       // the length of your string, due to x=0 being the first char in your string

  printf("There are %d total characters in %s\n", x, string); // Prints the final character count
  printf("The address of your string is %d\n", string); // The "%d" tells the computer to print the pointer value (memory address) as a number, 
                                                      // rather than dereferencing and printing as a string when "%s" is used.
   return 0;
} 
Will declare a string variable; fill it with your "1234de" string; traverse it, printing each character, while handling it as a char array; give the total character count before the '0' terminating character and print it as a string; then print the integer value of your "string" pointer, just to demonstrate part of how strings are handled in C.
(NOTE: Some compilers/IDEs will throw a "WARNING" when compiling the above code, due to "using a pointer as integer without a cast" "expected type is number, variable type is char *" ... you can disregard the warning for now, and know that it *is* a runnable example)

I hope this helps;
robherc

RE: Split string into individual characters

(OP)
Thank you! Very useful indeed and clearly explained.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close