Thread: printing half a char array

  1. #16
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    well i fixed my problem
    i used %.29s flag with printf to limit the number of chars printed and i removed that whole for loop and used
    Code:
    while (playtime[i]!=0)/* if playtime ==0 then there is no song there*/
    	{
    		mins = 0;/* tryint to make it obvious if convert method isnt working*/
    		secs = 0;/* by initializeing mins and secs or each new song to 0*/
    		convertSecondsToMinutes(&mins, &secs, playtime[i]);
    		
    		titles[i][(strlen(titles[i])-1)] = '\0'; /* this is how i found 
    								the newline char and replace 
    								it with null, then i just printed
    								a newline at the end of my real 	
    								printf statemnet*/
    		
    		if (select[i] == 1)
    		{
    			printf("*");/* indicates song is selected*/
    		}
    		else
    		{
    			printf(" ");/* holds place of * to keep spaceing same*/
    		}
    		printf("[%1d]%3d:%02d %.30s\n", i+1, mins, secs,  titles[i] );/* prints song info...*/
    		i++;

  2. #17
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    i used strlen() to find the end of the string and i knew the new line char was the second from last beacuse the null is last and i replaced it with null

  3. #18
    Registered User
    Join Date
    Nov 2006
    Posts
    10
    that didnt work as good as i hoped it would
    this found the newline and replaced it with null

    Code:
    for(j=0; j<30; j++)
    {
    	if(titles[i][j]=='\n')
    	{
    		titles[i][j] = '\0';
    	}
    }

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >titles[i][(strlen(titles[i])-1)] = '\0';
    That's not such a hot idea. A while back we tossed around a slew of ideas for removing the newline, and the usual consensus is that your best option is something like this:
    Code:
    void trim_newline ( char s[] )
    {
      char *newline = strrchr ( s, '\n' );
    
      if ( newline != NULL )
        *newline = '\0';
    }
    Of course, that assumes that there may be more than one newline and you want to snip the last one. If you know that there will only be one newline, such as with the result of fgets, you should probably be using strchr. Some implementations of strrchr can be noticeably slower.
    My best code is written with the delete key.

  5. #20
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Presumably because they use strlen() first. Then again, I've never really heard anything from the local shock jock with a newline in its name...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. help with array of char and char **
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 02:23 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM