LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   copy string a to string b and change string b with toupper() and count the chars (http://www.linuxquestions.org/questions/programming-9/copy-string-a-to-string-b-and-change-string-b-with-toupper-and-count-the-chars-839858/)

beep3r 10-22-2010 04:11 PM

copy string a to string b and change string b with toupper() and count the chars
 
///

paulsm4 10-22-2010 04:20 PM

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);


beep3r 10-22-2010 04:54 PM

with this code and the problem still exists (Segmentation fault)
 
///

paulsm4 10-22-2010 07:22 PM

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;
}



All times are GMT -5. The time now is 12:40 AM.