C programming debugging exercises assignments solutions



Share

Article background

When you are working on a c programming project and implementing different functionality as per the requirements of the project you will come accross issues during during source code development phase or integration testing phase or sytem test or field trials phase. So if you have good debugging skills that will help you to resolve the errors in your code and put your project in a clean state.

In this article we will help you to develop those debugging skills by using which you can write better code during development phase or resolve issues during integration/system level testing or field trials. I will give you here small code snippets having issues in it as a debugging exercise and you will try those execises to resolve the issue. I will also provide a link to the solution of each of the debugging exercise to verify your solution.

Subscribe to enable complete access to all , and .

This article will help you to gain required knowledge to successfully complete , which are required to complete and earn course completion certificates for software jobs.

Debugging exercises


Write a function to count the sum of all the digits present in an integer number

Exercise source code.
Write a function to count the sum of all the digits present in an integer number

#include <stdio.h>
int SumDigit(int );
int main()
{
    int res;
    res = SumDigit(1223);
    printf("The sum of digits %d\n", res);
    return 0;
}
int SumDigit(int num)
{
    int sum = 0,rem;
    while(num > 9)
    {
        rem = num%10;
        num /= 10;
    }
    sum += num;
    return sum;
}
Problem to debug.
    Output of the above debugging exercise is given below.
$ ./a.out
The sum of digits 1
$
    This debugging exercise has one issue. In this program we are counting the sum of all digits present in number 1223. So it should be 1+2+2+3 i.e. 8. But we are getting program output as 1. Try to solve this exercise.
    The solution for this debugging exercise is provided below.

#include <stdio.h>
int SumDigit(int );
int main()
{
    int res;
    res = SumDigit(1223);
    printf("The sum of digits %d\n", res);
    return 0;
}
int SumDigit(int num)
{
    int sum = 0,rem;
    while(num > 9)
    {
        rem = num%10;
        sum += rem;
        num /= 10;
    }
    sum += num;
    return sum;
}

Write a function to count number of characters in a given string

Exercise source code.
Write a function to count number of characters in a given string

#include <stdio.h>
void NumOfChars(char *s, int *);
int main() {
  char str[] = "Test string";
  int res;
  NumOfChars(str,&res);
  printf("Result is:%d\n", res);
}
void NumOfChars(char *s, int *r)
{
  int i = 0;
  while(s[i] != '\0')
  {
    *r = *r + 1;
    i++;
  }
}
Problem to debug.
    Output of the above debugging exercise is given below.
$ gcc CharCount.c
$ ./a.out
Result is:66339
$
    This debugging exercise has one issue. In this program we are counting the number of characters present in string "Test string". So excluding null character it should return as 11. But it is returning 66339. Try to solve this exercise.
    The solution for this debugging exercise is provided below.

#include <stdio.h>
void NumOfChars(char *s, int *);
int main() {
  char str[] = "Test string";
  int res = 0;
  NumOfChars(str,&res);
  printf("Result is:%d\n", res);
}
void NumOfChars(char *s, int *r)
{
  int i = 0;
  while(s[i] != '\0')
  {
    *r = *r + 1;
    i++;
  }
}

Write a function to count number of words present in a string

Exercise source code.
Program to write a function to count number of words present in a string

#include <stdio.h>
void NumOfWords(char *s, int );
int main() {
  char str[] = "Hello how are you";
  int res = 1;
  NumOfWords(str,res);
  printf("Result is:%d\n", res);
}
void NumOfWords(char *s, int r)
{
  int i = 0;
  while(s[i] != '\0')
  {
    if(s[i] == ' ')
    {
      r = r + 1;
    }
    i++;
  }
}
Problem to debug.
    Output of the above debugging exercise is given below.
$ ./a.out
Result is:1
$
    This debugging exercise has one issue. We are expected to count the number of words present in the string str which is passed to the function NumOfWords. So as string str has 4 words we should get the count as 4. But we are getting the count as 1. Try to solve this exercise.
    The solution for this debugging exercise is provided .

Write a function to loop through the strings present in an array of pointers to string

Exercise source code.
Write a function to loop through the strings present in an array of pointers to string

#include <stdio.h>
char *StringSet[] = {
    "string1 1 0 2 0",
    "string2 1 2 0 5 9",
    "string3 0 9 2",
    ""
};
int main(){
  int i = 0;
  while(StringSet[i] != NULL){
    printf("String %s\n",StringSet[i]);
    i++;
  }
}
Problem to debug.
    Output of the above debugging exercise is given below.
$ ./a.out
String string1 1 0 2 0
String string2 1 2 0 5 9
String string3 0 9 2
String
$
    This debugging exercise has one issue. In this program we are expected to get only string1, string2, string3 as output. But we are getting inside the while loop 4 times as is observed in the program output. Try to solve this exercise.
    The solution for this debugging exercise is provided .

To Read Full Article

You need to subscribe & logged in

Subscribe


Want to contribute or ask a new article? Write and upload your article information .
 | 
Share

 Articles

 C Programming

 Linux Software

 Embedded Systems

 Python Programming

 Search Code Snippets

 Popular C search examples

 Popular C search MCQs

 Online certificate courses

 Test your skills:online tests

 C Programming

 Python Programming

 Linux Software

 Quantitative Aptitude

 Embedded System Software