0% found this document useful (0 votes)
67 views3 pages

String Operations in C Programming

The document provides C programming examples for various string operations without using built-in string functions. It covers finding string length, copying, converting cases, comparing, concatenating, reversing, and extracting substrings. Each operation includes code snippets and their corresponding outputs.

Uploaded by

Jenifus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views3 pages

String Operations in C Programming

The document provides C programming examples for various string operations without using built-in string functions. It covers finding string length, copying, converting cases, comparing, concatenating, reversing, and extracting substrings. Each operation includes code snippets and their corresponding outputs.

Uploaded by

Jenifus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

String Operations (Without using string functions)

1. Finding String Length


#include <stdio.h>
void main() {
char s[] = "Programming is fun";
int i,length = 0;
for (i = 0; s[i] != '\0'; i++)
{
length++;
}
printf("Length of the string: %d", length);
}
Output:
Length of the string: 18

2. Copying String
#include <stdio.h>
void main()
{
char s1[] = "Programming is fun", s2[100], i;
printf("String s1 : %s\n", s1);
for (i = 0; s1[i] != '\0'; i++) {
s2[i] = s1[i];
}
printf("String s2 : %s", s2);
}
Output:
String s1 : Programming is fun
String s2 : Programming is fun

3. Converting String to Lower Case


#include <stdio.h>
void main() {
char s[] = "PROGRAMMING IS FUN";
int i;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='A'&& s[i]<='Z')
{
s[i] = s[i]+32;
}
}
printf("String in lowercase is:\n");
puts(s);
}
Output:
String in lowercase is:
programming is fun
4. Converting String to Upper Case

#include <stdio.h>
void main() {
char s[] = "programming is fun";
int i;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='a'&& s[i]<='z')
{
s[i] = s[i]-32;
}
}
printf("String in Uppercase is:\n");
puts(s);
}
Output:
String in Uppercase is:
PROGRAMMING IS FUN

5. Comparing two Strings

#include<stdio.h>
void main()
{
char str1[]="Programming", str2[]="Programming";
int d,i,len1=0,len2=0,flag=0;
for(i=0;str1[i]!='\0';i++)
len1++;
for(i=0;str2[i]!='\0';i++)
len2++;
if(len1!=len2)
printf("Given strings are different.");
else
{
for(i=0;i< len1;i++)
{
if(str1[i]!=str2[i])
{
flag=1;
}
}
if(flag==0)
printf("Given strings are same.");
else
printf("Given strings are Different");
}
}
Output:
Given strings are same.
6. String Concatenation
#include<stdio.h>
void main(){
char str1[50]="Programming", str2[]=" is fun";
int d,i,len=0;
for(i=0;str1[i]!='\0';i++)
len++;
for(i=0;str2[i]!='\0';i++)
{
str1[len+i] = str2[i];
}
printf("Concatenated string is: %s", str1);
}
Output:
Concatenated string is: Programming is fun

7. String Reverse
#include<stdio.h>
void main(){
char str[]="Programming",rstr[50];
int i,len=0;
for(i=0;str[i]!='\0';i++)
len++;
printf("Original string is: %s\n", str);
for(i=len-1;i>=0;i--)
{
rstr[len-1-i] = str[i];
}
rstr[len]='\0';
printf("Reversed string is: %s", rstr);
}
Output:
Original string is: Programming
Reversed string is: gnimmargorP

8. Extracting Substring from a String:


#include <stdio.h>
#include <string.h>
void main(){
char originalString[] = "Hello, World!",substring[10];
int i,start = 7,length = 5;
printf("Original string is: %s\n", originalString);
for(i=start;i<start+length;i++)
{
substring[i-start] = originalString[i];
}
substring[length]='\0';
printf("Sub string is: %s", substring);
}
Output:
Original string is: Hello, World!
Sub string is: World

You might also like