Practice Problems

Strings

Notice when you get compile errors (when gcc gives you an error)
vs. errors that crash the program (run-time errors)
vs. logical errors (compile and run fine but problem with your code)

  1. How do the loops differ for copying an array vs. a character array? For example, write a function that copies an int array to another int array and write a function that does it for a string (character) array. (Note the different loop structure that should be used, what the condition is to know when to stop looping, other things that need to be done)

    When copying a string, you stop when you get to the '\0' (and then make sure to copy the '\0'). When copying an int array, you typically copy the entire array.

  2. Write a function that takes a string as a parameter and returns the number of alpha-numeric characters in the string.
    	#include <string.h>	/* Assumed for subsequent examples */
    	#include <ctype.h>	/* Assumed for subsequent examples */
    
    	int count(char s[]) {
    		int count=0, i;
    		for (i=0; s[i] != '\0'; i++)
    			if (isalnum(s[i]))
    				count++;
    		return count;
    	}
    	
  3. Write a function that takes a string as a parameter and returns the number of vowels in the string.
    	int count(char s[]) {
    		int count=0, i;
    		for (i=0; s[i] != '\0'; i++) {
    			switch (s[i]) {
    			case 'a':
    			case 'e':
    			case 'i':
    			case 'o':
    			case 'u':
    			case 'y':	/* If you think 'y' is a vowel */
    				count++;
    			}
    		}
    		return count;
    	}
    	
  4. Write a function that takes a string as a parameter and REMOVES all spaces in the string (returns nothing, function should modify the original string passed to the function).
    	void despace(char s[]) {
    		int out=0, in;
    		for (in=0; s[in] != '\0'; in++) {
    			if (s[in] != ' ')
    				s[out++] = s[in];
    		}
    		s[out] = '\0';
    	}
    	
  5. Write a function that takes a string as a parameter and returns the number of sentences. Assume a sentence ends in either a period, ! or ?
    	int count(char s[]) {
    		int count=0, i;
    		for (i=0; s[i] != '\0'; i++) {
    			char c = s[i];
    			if (c=='.' || c=='!' || c=='?') {
    				char c2 = s[i+1];
    				if (c2==' ' || c2=='\0') {
    					count++;
    				}
    			}
    		}
    		return count;
    	}
    	
  6. Write a function that takes a string as a parameter and returns the number of words in the string.

    This is quite similar to #5. Where #5 counted the ends of sentences (one of .!? followed by a space or '\0'), we would count the ends of words (a non-space followed by a space or '\0'),

  7. What is the result of
     strcmp( "april", "April" )
    In ASCII, 'a' comes after 'A'. Hence, "april" is greater than "April", so strcmp will return a number greater than zero. Do this command to read about ASCII: man ascii
  8. How do you convert a character to upper case? (What is the function you need to call, and what other header file do you need to import)
    	#include <ctype.h>
    	...
    	char c = ...;
    	char upper = toupper(c);
    	
  9. Show three different ways to initialize the variable name with "Cookie".
    	char name[] = "Cookie";
    
    	char name[] = {'C','o','o','k','i','e'};
    
    	char name[7];
    	strcpy(name, "Cookie");
    	
  10. What's the difference between strcat and strcpy ?

    strcat(a,b) appends b to the end of a.
    strcpy(a,b) copies b to a, replacing a's previous value.

  11. If you know the string variable filename ends with .txt how can you remove the extension from the string?
    	filename[strlen(filename)-4] = '\0';
    	/* Why 4?  Because .txt is four characters. */
    	
  12. Using fgets, read in a character and 3 integers and store them in to appropriate variables.
    	char buf[80];
    	char c;
    	int i,j,k;
    	fgets(buf, sizeof(buf), stdin);
    	sscanf(buf, "%c%d%d%d", &c, &i, &j, &k);
    	
  13. Write the code to print out the size of the string in a variable named title.
    	printf("size: %d\n", strlen(title));
    	
  14. What happens if your string does not contain a null character in the array?

    Whatever code is processing the string will simply continue until it finds a '\0', somewhere. It'll either find one, or it will hit the end of your allocated address space, and then your program will fail with a confusing error message.

  15. Convert the string variable score to an integer value using the function atoi
    	int value = atoi(score);
    	
  16. What is the function to call to determine whether a character is a digit or not?

    isdigit