Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

STRING PROGRAMS:

1. 1. C program to print indexes of a particular character in a string.


This program will read a string and a character to find its indexes where character exists in C
programming language.

2. C program to compare two strings using pointers


Learn: How to compare two strings using pointers in C programming language?

Here, you will learn how to read and compare two strings using pointers in C programming
language? In this program, we are taking two strings with maximum number of characters (100)
MAX using pointers and comparing the strings character by characters.

3. C program to create and print array of strings


Learn: How to create, read and print an array of strings? This program will demonstrate usages of array
of strings in C programming language.

We have posted programs on strings in C language, now in this post we are going to discuss about array
of strings in C.

4.C program to capitalize first character of each word in a


string
In this program, we will learn how to capitalize each word of input string using C program?

This program will read a string and print Capitalize string, Capitalize string is a string in which first
character of each word is in Uppercase (Capital) and other alphabets (characters) are in Lowercase (Small).

For example:
If input string is "hello friends how are you?" then output (in Capitalize form) will be "Hello Friends
How Are You?".

5. C program to find the frequency of a character in a


string
In this program, we will learn how to find occurrence of a particular character in a string using C
program?

Here, we are reading a character array/string (character array is declaring with the maximum number of
character using a Macro MAXthat means maximum number of characters in a string should not more
than MAX (100), then we are reading a character to find the occurrence and counting the characters which
are equal to input character.

For example:
If input string is "Hello world!" and we want to find occurrence of 'l'in the string, output will be 'l' found
3 times in "Hello world!".

6. C program to read a string and print the length of the


each word
In this program, we will learn how to count length of each word in a string in C language?

There are many string manipulation programs and string user defined functions, this is an
another program in which we will learn to count the length of each word in given string.

In this exercise (C program) we will read a string, like "Hi there how are you?" and it will print the word
length of each word like 2, 5, 3, 3, 4.

Input
Hi there how are you?Output
2, 5, 3, 3, 4

7. C program to eliminate/remove all vowels from a


string
Here, we will learn how to eliminate/remove all vowels from a string?, in this program we are reading a
string of 100 (maximum) characters, and printing the string after eliminating vowels from the string.

Here, we designed two user defined functions:


1) char isVowel(char ch)
This function will take a single character as an argument and return 0 if character is vowel else it will
return 1.

2) void eliminateVowels(char *buf)


This function will take character pointer (input string) as an argument and eliminate/remove all vowels in
it.

Logic to implement:

1. Run a parent loop from 0 to NULL.


2. Chech the character, whether it is vowel or not, if the character is vowel shift all characters to the
left.
3. To shift all characters to the left, run another loop from "i" (Parent loop's loop counter) to NULL.

8. C program to eliminate/remove first character of each


word from a string
In this program, we will learn how to eliminate/remove first character of each word in a string? Here,
we will read a string and eliminate its first character of each word, then print new string (after elimination
of first character of each words).

Logic to implement

1. Eliminate first character of the string (first character of first word) by shifting other character to
the left.
2. Eliminate the first character of other word (by checking whether there is an index has space and
index+1 (next to index where space found) non space and shift other characters to the left.
3. Run this process until NULL not found in the parent loop.

9. C program to read n strings and print each string's


length
In this article, we will read n strings from the input and store it in array and print the length of each
string in the array.
Submitted by Abhishek Pathak, on October 24, 2017
In this article, we will create a C program that will be based on Strings. We will read "n" number of
strings, which will be specified by the user and store it in 2D array. Then we will print each string
with their length side by side. This is an interesting program that deals with the Array of strings and how
to perform string operations such as strlen() string length on them.

10. C program to copy one string to another and count


copied characters
In this C program, we are going to learn how to copy one string to another without using any library
function? Here, we will also get the total number of copied characters and printing them.
Submitted by IncludeHelp, on March 14, 2018

Given a string and we have to copy it in another string, also find and print the total number of
copied characters using C program.

Example:

Input:
String 1 (Input string): "This is a test string"

Now, we will copy string 1 to the string 2


Output:
String 1: "This is a test string"
String 2: "This is a test string"
Total numbers of copied characters are: 21

Logic:

Copy character by character from string 1 to the string 2 and use a loop counter which is initialized by 0,
after copying each character, increase the loop counter. At the end, assign NULL (‘\0’) to the target (string
2) and loop counter will be the total number of copied characters.

In the given program, we have created a user defined function to copy one string to another string and
returning the total number of copied characters.

Function prototype: int copy_string(char *target, char *source)

Where,

 Parameters:
o char *target - pointer to target string in which we will copy the string
o char *source - pointer to source string whose value we want to copy
 return type: - int : total number of copied characters
11.

C program to remove all spaces from a given string


In this C program, we are going to learn how to remove all spaces from a given string? Here, we will
have a string with spaces and program will remove all spaces and print the string without spaces.
Submitted by IncludeHelp, on April 05, 2018

Given a string with spaces and we have to remove all spaces from it using C program.

Without using other temporary string, in this program - we are reading a string from the user and then
printing the string (by updating the same string variable) after removing all spaces from it.

Example:

Input:
String: "C is the master of all"
Output:
String: "Cisthemasterofall"

Input:
String: "I love includehelp.com"
Output;
String: "Iloveincludehelp.com"

12.

C program to convert a string to sentence case


In this C program, we are going to learn how to convert a given string to sentence case? To implement
this logic, we are creating a user define function to do the same.
Submitted by IncludeHelp, on April 05, 2018

Given a string and we have to convert it to sentence case using C program.

So, first of all, we have to understand - what is a sentence case? A string is in a sentence case, if first
character of each sentence is an uppercase character.

Example:
Input string: "hello how are you?"
Output:
Sentence case string is: "Hello how are you?"

Input string: "hello. how are you?"


Output:
Sentence case string is: "Hello. How are you?"

13.

C program to remove alphabets from an alphanumeric


string
In this C program, we are going to learn how to remove alphabets from a given alphanumeric string?
If string contains alphabets and numbers both, this program will remove only alphabets from the string.
Submitted by IncludeHelp, on April 05, 2018

Given a string and we have to remove it all alphabets using C program.

If string contains alphabets and numbers both (or anything with the alphabets), program to check the
alphabets, if found then program will remove them from the string.

Example:

Input:
String is: "hello123"
Output:
Final string is: "123"

Input:
String is: "Call 100"
Output:
Final string is: "100"

14.

You might also like