String Assignment
Name – priyanshu kumar
Roll no – 124104103
Section – EE_A_01
Submitted to – Dr. Yogita Ma’am
1. Code to copy a
source string into a
target string
Input
#include<stdio.h> void
xstrcpy(char*t, char*s){
while(*s!='\0'){
*t = *s;
s++;
t++;
*t = '\0';
int main(){
int n,m;
printf("Enter the string(limit 10)\n");
char s[11]; char t[12]; scanf("%s",
s); xstrcpy(t,s);
printf("Original string is - %s\n",s);
printf("Copied string is - %s",t);
return 0;
Output
2. Code to find length
of string
Input
#include<stdio.h> int
str_length(char *str){
int len = 0;
while(*str != '\0'){
len ++;
str ++;
return (len);
int main (){
char str[101];
printf("Enter a string Limit(100)\n"); scanf("%s", str);
printf("Length of entered string is %d",str_length(str));
return 0;
Output
3. Code to swap the
cases of a given
string
Input
#include<stdio.h> void
swap_str(char*str){
int i;
for(i=0; i<19; i++){
if(str[i] <= 'Z' && str[i] >= 'A')
str[i] = str[i] + 32;
else if(str[i] <= 'z' && str[i] >= 'a')
str[i] = str[i] - 32;
}
}
int main(){
char str[] ="Priyanshu_is_nitian \0";
swap_str(str); printf("Swapped
string is\n%s", str);
Output
4. Code to
concatenate two
strings in a new
string
Input
#include<stdio.h> void str_cat(char*s1,
char*s2, char*concat){
while(*s1 !='\0'){
*concat = *s1;
s1 ++; concat ++;
while(*s2 != '\0'){
*concat = *s2;
s2 ++;
concat++;
*concat = '\0';
int main(){ char s1[11]; char s2[16]; char concat[26];
printf("Enter First string Limit(10)\n"); scanf("%s", s1);
printf("Enter Second string Limit(15)\n");
scanf("%s", s2); str_cat(s1,s2,concat);
printf("String after concatenation is \n%s", concat);
return 0;
Output
5. Code to compare
two strings
Input
#include<stdio.h> int
str_cmp(char *s1, char* s2){ int
x = 0;
while(*s2 != '\0'){
x =*s1 - *s2;
s1++;
s2++;
if(x!=0)
break;
return(x);
int main(){
char s1[11], s2[16]; printf("Enter the
First string. Limit(10)\n"); scanf("%s", s1);
printf("Enter the Second string. Limit(15)\n");
scanf("%s", s2); int x = str_cmp(s1,s2);
if(x==0)
printf("You entered same strings.");
else
printf("You entered two different strings.");
return 0;
}
Output
1.
2.
6. Code to find the
first appearance of
a character
Input
#include<stdio.h> int
str_occ(char* str){
while(*str != '\0'){
if(*str<'0' || *str>'9')
return (*str);
str++;
return (*str);
int main(){
char str[16];
printf("Enter a string. Limit (15)\n");
scanf("%s", str); int temp =
str_occ(str); if(temp =='\0')
printf("You entered digits in the string.");
else
printf("First character appeared is %c", temp);
Output
1.
2.
7. Code to reverse a
string
Input
#include<stdio.h> void
rev_str(char* rev, char*str){
int len =0; while(*str != '\
0'){
len++;
str++; }
int i;
for(i=0; i<=len; i++){
str--;
*rev = *str;
rev++;
*rev = '\0';
for(i=0;i<=len;i++)
rev--;
int main(){
char str[11];
char rev[11];
printf("Enter a string. Limit(10)\n");
scanf("%s", str); rev_str(rev,str);
printf("Original string was - %s\n",str);
printf("Reversed string is - %s",rev);
return 0;
Output