String formatting

This is a discussion on String formatting within the C Programming forums, part of the General Programming Boards category; char word[30] = "hello||how||are||you||doing||there" how can i break the string and display the following output ?? hello| |how| |are| |you| ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    String formatting

    char word[30] = "hello||how||are||you||doing||there"


    how can i break the string and display the following output ??

    hello|
    |how|
    |are|
    |you|
    |doing|
    |there

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Neglecting any invalid input, you could simply traverse the whole string and preint each character 1 by 1. If you encounter a |, print a newline then skip the following character (assuming they always comes in pairs of 2).
    Code:
    char* Pointer = word;
    while(*Pointer != '\0')
    {
      if(*Pointer == '|')
      {
        printf("\n");
        Pointer++; //Skips extra character
      }
      else
      {
        printf("%c", *Pointer);
      }
      Pointer++;
    }
    Note: Code has not been tested.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Code:
    int main() {
    	char *word = "hello||how||are||you||doing||there";
    	
    	while (*word != '\0') {
    		if (*word != '|') {
    			printf("%c", *word);
    			word++;
    		} else if (*word == '|') {
    			printf("%c", *word);
    			word++;
                            printf("\n%c", *word);
                            word++;
    		}
    	}
                    return 0;
    }
    Last edited by OOPboredom; 03-17-2004 at 08:27 AM.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char word[] = "hello||how||are||you||doing||there";
      char* Pointer = word;
      while(*Pointer != '\0')
        {
          printf("%c%s", *Pointer, *Pointer == '|' && *(Pointer+1) == '|' ? "\n" : "");
          Pointer++;
        }
      return 0;
    }
    Last edited by major_blagger; 03-17-2004 at 08:19 AM.

  5. #5
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    Great solution major. Reminds me of a professor in college that took a simple solution to a problem and wrote a small portion of code for it in C.

    Each time through he would re-write the code and simplify or concatendate certain parts of the code until he got to only 2 lines of code for a solution that had 10 lines originally.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... or if you can live without the | characters....:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char word[] = "hello||how||are||you||doing||there";
      char *p;
      
      p = strtok (word, "|");
      
      while (p)
      {
        puts (p);
        p = strtok (NULL, "|");
      }  
      
      return(0);
      
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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