LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-19-2004, 02:05 PM   #1
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 122

Rep: Reputation: 15
Fun with strings & chars in C


Im programming in C.
I have an int variable called z. I have a string variable called spaces. I would like to format "spaces" with "z" number of space chars. I've tried loops and stuff but cant get it right. Any ideas?

For example, z = 8. Would like "spaces" to contain 8 spaces or ' ' chars.

Thanks!!
 
Old 05-19-2004, 02:20 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
The easiest way is to use memset().

Code:
char *make_spaces(int z)
{
  static char spaces[1024]; // Or some other sufficiently large number

  memset(spaces, ' ', z);
  spaces[z] = '\0';  // Make sure you NUL-terminate the string.

  return spaces;
}
Here's an example using a loop if you prefer that:

Code:
char *make_spaces(int z)
{
  static char spaces[1024];
  int i;

  for(i = 0;i < z;++i)
    spaces[i] = ' ';
  spaces[z] = '\0';

  return spaces;
}
And another example without the need for the i counter:

Code:
char *make_spaces(int z)
{
  static char spaces[1024];

  spaces[z] = '\0';
  while(--z >= 0)
    spaces[z] = ' ';

  return spaces;
}

Last edited by itsme86; 05-19-2004 at 02:26 PM.
 
Old 05-19-2004, 03:38 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
It would be a good idea to note that you should be careful about using these examples more than once, e.g.
Code:
char *a, *b;
a = make_spaces(3);
b = make_spaces(5);
printf("a is %d spaces long\n", strlen(a)); /* prints "a is 5 spaces long\n" */
If that is a problem, you need to either take a char* as an argument (and assume it's sufficiently long) or return a malloc()d string, leaving the caller to free it.
 
Old 05-19-2004, 03:39 PM   #4
Scrag
Member
 
Registered: Mar 2004
Location: Wisconsin
Distribution: Kali Linux
Posts: 122

Original Poster
Rep: Reputation: 15
Wow.....three examples

Thanks again for he help!
 
Old 05-19-2004, 04:03 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Thank you, aluser. I forgot to mention that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 05:04 AM
how dyou change 'ok' & 'cancel' strings to 'sure' & 'nope' and stuff like that(gnome) xenithi Linux - Newbie 3 11-25-2004 01:10 AM
most fun & excited things about Linux woranl Linux - General 2 07-27-2004 09:28 PM
C....Fun with strings :( Scrag Programming 3 04-25-2004 03:12 PM
Fun with linux && Linus out and about. itsjustme General 5 03-24-2004 05:18 PM


All times are GMT -5. The time now is 10:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration