LinuxQuestions.org
Help answer threads with 0 replies.
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 10-22-2010, 04:11 PM   #1
beep3r
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
Question copy string a to string b and change string b with toupper() and count the chars


///

Last edited by beep3r; 10-23-2010 at 05:35 AM.
 
Old 10-22-2010, 04:20 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Two problems off the bat:
1. You're not initializing i
2. You're not always incrementing i

Here's an alternative:
Code:
int 
capitalize(char a[], char b[])
{
  int i, ct = 0;
  for (i=0; i < strlen (a); i++)
  {
    b[i] = toupper (a[i]);
    if (b[i] != a[i])
      ct++;
  }
  b[i] = '\0';
  return (ct);
 
Old 10-22-2010, 04:54 PM   #3
beep3r
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Question with this code and the problem still exists (Segmentation fault)

///

Last edited by beep3r; 10-23-2010 at 05:35 AM.
 
Old 10-22-2010, 07:22 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Uh - you need to allocate STORAGE for b[].

As you have it, both a[] and b[] are READ-ONLY.

Code:
int
main(int argc, char *argv[])
{
  int n_changes;
  char b[80];

  n_changes = capitalize("programming in C", b);
  printf("the number of changed chars is: %d, the updated string is: %s\n", 
    n_changes, b);

  return 0;
}

Last edited by paulsm4; 10-22-2010 at 07:25 PM.
 
  


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 do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
change case of a string (TOUPPER) in shell script raees Linux - Software 3 05-03-2005 02:13 PM


All times are GMT -5. The time now is 09:36 PM.

Main Menu
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