C-Style String Worksheet Two


  1. Write a function Equal to test whether two strings are equal. Equal should return 1 if the two string parms contain the same chars, and return 0 otherwise.
  2. Write a function that has two parms: a string and a char. The function should return the subscript of the first occurrence of the char, or a -1 if the char is not in the string. For example, if the string is "yes i can" and the char is 'c', the function should return 6.
  3. Write a function trimfront that removes all leading blanks from the string passed to it.
  4. What does the following code print?
      char line[80] = "this is a test; IT IS ONLY A TEST";
      char first[21];
      char sec[21];
      char third[31];
      cout << "line is: " << line << endl;
      strncpy(first,line,4);
      first[4] = '\0';
      cout << "length of first: " << strlen(first) << endl;
      cout << "first is: " << first << endl;
      strcat(line,first);
      cout << "line is: " << line << endl;
      strncpy(sec,line,14);
      sec[14] = '\0';
      strcpy(third,sec);
      for (int i = 0 ; i < strlen(third) ; i++)
        third[i] = toupper(third[i]);
      cout << "third is: " << third << endl;
      cout << "sec is: " << sec << endl;
    
  5. Write a function int concat(char str1[],char str2[],int maxlen) that uses strlen, strcat, and strncat. Your concat function should perform a complete concatenation of str2 onto the end of str1 only if the length of the concatenated string will not exceed maxlen-1 (the amount of room in str1). If the concatenated string will exceed maxlen-1 chars, concatenate only the first part of str2 so that the result will have length maxlen-1.



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002