Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

How to write string programs in C

Last Updated on June 20, 2022 by Ria Pathak

A String in C is basically a sequence of characters/symbols which is terminated with a null character \0.

E.g., char str = “this is a string”

Let us see how to declare a string in C.
Declare string variable

char str[16] = "This is a string" 
char str[] = "This is a string" 

Taking Inputs –

  • scanf() : this function is used to take string inputs in C.
    char str[30] ; scanf(%s,”This is a string”)
    This function is useful only when our string does not contain any spaces. In the above statement only “This” will be taken from the user.
  • fgets() : we can use this function even if we have spaces. Mostly this function is followed by a puts() function to print the entered string.

Code Implementation –

#include <stdio.h>
int main(void) {
    // char str[30];
    // scanf("%s",str);
    // printf("%s",str);
    char str1[30];
    fgets(str1,sizeof(str1), stdin);
    puts(str1); //prints the string
    return 0;
}
Output: This is a string

Accessing each elements of a string in C

Well, each of the characters are stored in a position contiguously. So, if we find out the length of the string then we can simply use a for loop to iterate through the elements of the string. We use strlen() function of string header file in C to get the length of our string.

Code Implementation –

#include <stdio.h>
#include <string.h>
int main(void) {
    char str[30];
    fgets(str,sizeof(str), stdin);
    //acessing each elements of the string
    for(int i = 0;i<strlen(str);i++)
        printf("%c",str[i]);
    return 0;
}

Now when we know to write basic program lets see an example –

Suppose we are given a string and we want to find the number of spaces in the string.
The idea is to iterate over the string elements and whenever we get a ‘(space) ‘character we increase our count.

Code Implementation –

#include <stdio.h>
#include <string.h>
int main(void) {
    char str[30];
    fgets(str,sizeof(str), stdin);
    int count = 0;
    //acessing each elements of the string
    for(int i = 0;i<strlen(str);i++)
    {
        if(str[i] == ' ')
            count++;
    }
    printf("%d", count);
    return 0;
} 

Time Complexity: O(length(str))

Another interesting example can be concatenating two strings.
We can do this manually by adding the characters of string 2 to the end of string 1.
Or we can directly use the strcat() function to do this. This function takes two arguments string 1 and string 2 and concatenates them and stores the result in string 1.

Code Implementation –

#include <stdio.h>
#include <string.h>
int main() {
  char s1[100] = "This is string1 ", s2[] = "This is string 2";
  int length, j;
  length = strlen(s1);
  for (j = 0; s2[j] != '\0'; ++j, ++length) {
    s1[length] = s2[j];
  }
//or simply
//strcat(s1,s2);
  s1[length] = '\0';
  puts(s1);
  return 0;
}

Time Complexity: O(length(str))

This article tried to discuss how to implement string in C langauage. Hope this blog helps you understand the implementation. To practice more problems you can check out MYCODE | Competitive Programming.

Leave a Reply

Your email address will not be published. Required fields are marked *