Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split string into individual characters 3

Status
Not open for further replies.

maharg

Technical User
Mar 21, 2002
184
0
0
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!
 
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;
};
 
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
 
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) .
 
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
 
Thank you! Very useful indeed and clearly explained.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top