logo codesdope
Level 1
1.
What would be the output of the following code:
#include <stdio.h>
int main(){
  char s[ ] = "Hello World" ;
  char t[25] ;
  char *ss, *tt ;
  ss = s ;
  while ( *ss != '\0' ){
    *ss++ = *tt++ ;
  }
  printf ( "%s\n", t ) ;
}
2.
What infromation does the name of an array store?
3.
Try to write your own functions for all the pre-defined fuctions of string.h given in the table of the chapter.
Level 2
1.
Write a program to print a string entered by user.
Answer
#include <stdio.h>
int main()
{
  char name[25]; 
  printf("Enter your name\n");
  scanf("%s", name); 
  printf("Your name is %s\n", name); 
  return 0;
}
									

2.
Write a program to print every character of a string entered by user in a new line using loop.
Answer
#include <stdio.h>
#include <string.h>
int main()
{
  char name[25];
  int i; 
  printf("Enter your name\n");
  scanf("%s",name); 
  for(i=0;i<strlen(name);i++)
  {
    printf("%c\n",name[i]);
  }
  return 0;
}
									

3.
Write a program to input and display the sentence I love candies.

4.
Write a program to find the length of the string "refrigerator".
Hint
Use strlen

5.
Create an array of characters and then print the address of each of the elements of the array. Take difference of two consecutive addresses and compare this with array of integers.
Hint
The differences will be the size of char and int respectively as continous memory is allocted in C.

6.
Write a program to enter a string s1 and copy it to another string s2.
Hint
Use strcpy or make your own function using for loop and copy each character.

7.
Write a program to compare if the two strings entered by user are equal or not without using predefined String functions.
Answer
#include <stdio.h>
#include <string.h>
int main()
{
  char s1[25];
  char s2[25];
  int i, equal = 1; 
  printf("Enter first string\n");
  scanf("%s",s1);
  printf("Enter second string\n");
  scanf("%s",s2);
  //length must be equal 
  if(strlen(s1)!=strlen(s2))
  {
    equal = 0;
  }
  else
  {
    for(i = 0;i<strlen(s1);i++)
    {
      if(s1[i]!=s2[i])
      {
        equal = 0;
        break;
      }
    }
  }
  printf("%d\n",equal);
  return 0;
}
									

8.
Write a program to check if the letter 'e' is present in the word 'Umbrella'.

9.
Write a program to check if the word 'orange' is present in the "This is orange juice".
Answer
#include <stdio.h>
#include <string.h>
int main()
{
  char s1[] = "orange";
  char s2[] = "This is orange juice";
  char s3[25];
  int i,j,present = 0;
  
  for(i=0;i<strlen(s2)-strlen(s1)+1;i++)
  {
    //making s3 equal to the substring of s2 from index j to j+length of s1
    for(j=0;j<strlen(s1);j++)
    {
      s3[j] = s2[j+i];
    }
    if(strcmp(s1,s3) == 0)
    {
      present = 1;
      break;
    }
  }

  printf("%d\n",present);
  return 0;
}
									

10.
Write a program to find the first and the last occurence of the letter 'o' and character ',' in "Hello, World".
Hint
For the last occurence, iterate from end.

11.
Write the string after the first occurrence of ',' and the string after the last occurrence of ',' in the string "Hello, Good, Morning".
Answer
#include <stdio.h>
#include <string.h>
int main()
{
  char s1[] = "Hello, Good, Morning";
  int first_ocr,last_ocr,i;
  for(i = 0;i<strlen(s1);i++)
  {
    if(s1[i] == ',')
    {
      first_ocr = i;
      break;
    }
  }
  for(i = strlen(s1)-1;i>=0;i--)
  {
    if(s1[i] == ',')
    {
      last_ocr = i;
      break;
    }
  }
  for(i = first_ocr+1;i<strlen(s1);i++)
  {
    printf("%c",s1[i]);
  }
  printf("\n");
  for(i = last_ocr+1;i<strlen(s1);i++)
  {
    printf("%c",s1[i]);
  }
  printf("\n");
  return 0;
}
									

12.
Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is. For example, if your name is Robert Brett Roser, then the output should be R.B.Roser.
Hint
1. Start from the end.
2. Check for the last word. There will be a space before the first character of the last word or the first character will also be the first character of the string if the string contains only one word.
3. Copy this last word in a new string.
4. Repeat step 2 for the middle word but this time only insert the first character and a dot(.) instead of the whole word in the new string.
5. Repeate till the loop ends by encountering the first character of the string.

Level 3
1.
Write a program to reverse a string with and without using strrev.
Answer
#include <stdio.h>
#include <string.h>
int main()
{
  char s1[] = "ABCD";
  char s2[25];
  int i;

  for(i = 0;i<strlen(s1);i++)
  {
  	s2[strlen(s1)-i-1] = s1[i];
  }

  printf("%s\n",s2);
  
  return 0;
}
									

2.
Write a program to find the length of a string without using predefined String functions.
Hint
Last character is '\0'.

3.
Check the occurrence of the letter 'e' and the word 'is' in the sentence "This is umbrella" without using predefined String functions.

4.
Write a program to find the number of vowels, consonents, digits and white space characters in a string

5.
Write a program to concatenate two strings "Hello" and "World" without using strcat.
Answer
#include <stdio.h>
#include <string.h>
int main()
{
  char s1[25] = "Hello";
  char s2[] = "World";
  int i,j=0;

  for(i = strlen(s1);i<10;i++)
  {
  	s1[i] = s2[j];
  	j++;
  }
  printf("%s\n",s1);

  return 0;
}
									

6.
Write a program to find out the largest and smallest word in the string "This is an umbrella".

7.
Write a program to check if a given string is a Palindrome.
A palindrome reads same from front and back e.g.- aba, ccaacc, mom, etc.
Answer
#include <stdio.h>
#include <string.h>
int main()
{
  char s1[] = "aba";
  int i;
  int pali = 1;

  for(i=0;i<strlen(s1)/2;i++)
  {
  	if(s1[i] != s1[strlen(s1)-i-1])
  	{
  		pali = 0;
  		break;
  	}
  }

  printf("%d\n",pali);

  return 0;
}
									

8.
Write down the names of 10 of your friends in an array and then sort those in alphabetically ascending order.
Hint
You can use any sorting algorithm (like one in the practice section of the chapter array) and compare using strcmp function.

9.
Write a program to delete all the consonents from the string "Hello, have a good day".

10.
Write a program to delete the word "the" in the sentence "This is the lion in the cage".

11.
Write a program to check if the two strings entered by user are anagrams or not. Two words are said to be anagrams if the letters of one word can be rearranged to form the other word. For example, jaxa and ajax are anagrams of each other.
Answer
A better approach would be to map every character of english alphabets with elements of an array having 26 (or 26*2 considering cases) elements representing each alphabet. Try it yourself.
#include <stdio.h>
#include <string.h>
int main()
{
  char s1[] = "ajax";
  char s2[] = "jaxa";
  int i,j;
  int anagram = 1;

  //only if both strings have same length
  if(strlen(s1)!=strlen(s2))
  {
    anagram = 0;
  }
  else
  {
    //matching every element of s2 with every element of s1
    for(i = 0;i<strlen(s1);i++)
    {
      int found = 0;
      for(j = 0;i<strlen(s2);j++)
      {
          if(s1[i] == s2[j])
          {
            //if equal then equating it to empty char
            //so doesn't match again
            s2[j] = ' ';
            found = 1;
            break;
          }
      }
      //if not found then it is not an anagram
      if(found == 0)
      {
        anagram = 0;
        break;
      }
    }
  }

  printf("%d\n",anagram);

  return 0;
}
									

Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.