0% found this document useful (0 votes)
19 views5 pages

String Program

The document contains 8 questions and programs related to string manipulation in C without using string library functions. The questions cover calculating string length, checking for palindromes, copying strings, counting words, comparing strings, and reversing strings. The programs provided as answers to each question demonstrate how to perform these string operations using only basic C constructs like for loops and character arrays instead of functions from the string header file.

Uploaded by

Tanmay Vashishth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
19 views5 pages

String Program

The document contains 8 questions and programs related to string manipulation in C without using string library functions. The questions cover calculating string length, checking for palindromes, copying strings, counting words, comparing strings, and reversing strings. The programs provided as answers to each question demonstrate how to perform these string operations using only basic C constructs like for loops and character arrays instead of functions from the string header file.

Uploaded by

Tanmay Vashishth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Q1.

Calculate Length of String without Using strlen() Function


#include <stdio.h>
Void main()
{
char s[] = "Programming is fun";
int i;

for (i = 0; s[i] != '\0'; ++i);

printf("Length of the string: %d", i);


getch();
}
Explanation
Here, using a for loop, we have iterated over characters of the string from i = 0 to until
'\0' (null character) is encountered. In each iteration, the value of i is increased by 1.
When the loop ends, the length of the string will be stored in the i variable.

Q2. program to check whether a string is palindrome without using string function
#include <stdio.h>
#include <string.h>

int main()
{
char text[20], reverse_text[20];
int i,n, length = 0;

printf("Enter text: ");


gets(text);

for (i = 0; text[i] != '\0'; i++)


{
length++; //this will calculate the length of given text
}
//Reverse the original text and store into reverse_text
for (i = length - 1; i >= 0; i--)
{
reverse_text[length - i - 1] = text[i];
}

//Check whether reverse_text is same to original text


for (n = 1, i = 0; i < length; i++)
{
if (reverse_text[i] != text[i])
n = 0;
}
if (n == 1)
printf("%s is a palindrome.", text);
else
printf("%s is not a palindrome", text);

return 0;
}

Q3. program to check whether a string is palindrome or not using string function
#include <stdio.h>
#include <string.h>

void main()
{
char string[50], string_reverse[50];

printf("Enter the string: ");


gets(string);

strcpy(string_reverse, string); //copy string into string_reverse


strrev(string_reverse); //reverse the value of string_reverse

if (strcmp(string, string_reverse) == 0) //compare both string


printf("The given string is a palindrome.\n");
else
printf("The given string is not a palindrome.\n");

getch();
}

Q4. Copy String Without Using strcpy()


#include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
gets(s1);

for (i = 0; s1[i] != '\0'; ++i) {


s2[i] = s1[i];
}

s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
Q5. Count the total number of words in a string

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
void main()
{
char str[str_size];
int i, wrd;
printf("\n\nCount the total number of words in a string :\n");
printf("------------------------------------------------------\n");
printf("Input the string : ");
gets(str);
i = 0;
wrd = 1;

/* loop till end of string */


while(str[i]!='\0')
{
/* check whether the current character is white space or new line or tab character*/
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
wrd++;
}

i++;
}

printf("Total number of words in the string is : %d\n", wrd-1);


getch();
}

Q6. Write a program in C to compare two string without using string library functions

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define str_size 100 //Declare the maximum size of the string

void main()
{
char str1[str_size], str2[str_size];
int flg=0;
printf("\n\nCompare two string whether they are equal or not :\n");
printf("------------------------------------------------------\n");
printf("Input the 1st string : ");
gets(str1);
printf("Input the 2nd string : ");
gets(str2);

int i=0;

/* Runs till both strings are equal */


while(str1[i] == str2[i])
{
if(str1[i] == '\0' || str2[i] == '\0')
break;
i++;
}
if(str1[i-1] == '\0' && str2[i-1]=='\0')
flg=0;
else if(str1[i] > str2[i])
flg=1;
else if(str1[i] < str2[i])
flg=-1;

if(flg == 0)
{
printf("\nThe length of both strings are equal and \nalso both strings are
equal.\n\n");
}
else if(flg == -1)
{
printf("\nThe length of the first string is smaller than second.\n\n");
}
else
{
printf("\nThe length of the first string is greater than second.\n\n");
}
getch();
}

Q7. Write a program in C to copy one string to another string.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main()
{
char str1[100], str2[100];
int i;
printf("\n\nCopy one string into another string :\n");
printf("-----------------------------------------\n");
printf("Input the string : ");
gets(str1);

/* Copies string1 to string2 character by character */


i=0;
while(str1[i]!='\0')
{
str2[i] = str1[i];
i++;
}

//Makes sure that the string is NULL terminated


str2[i] = '\0';

printf("\nThe First string is : %s\n", str1);


printf("The Second string is : %s\n", str2);
printf("Number of characters copied : %d\n\n", i);
}

Q8. program to Reverse a String without using string function


#include <stdio.h>
#include <string.h>

void main()
{
char Str[100], RevStr[100];
int i, j, len;
printf("\n Please Enter any String : ");
gets(Str);
j = 0;
len = strlen(Str);
for (i = len - 1; i >= 0; i--)
{
RevStr[j++] = Str[i];
}
RevStr[i] = '\0';
printf("\n String after Reversing = %s", RevStr);

getch();
}

You might also like