0% found this document useful (0 votes)
4 views

pps week 10 h

I have uploaded 5 doc

Uploaded by

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

pps week 10 h

I have uploaded 5 doc

Uploaded by

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

____________________________________________________________________________

Programming For Problem Solving Lab


WEEK-10
NAME : B Harshitha DATE :17.12.2024

HTNO : 24R21A05D6

BRANCH : CSE

SECTION : D

PROBLEM STATEMENT:

Write a program to copy the contents of one string into another


using strcpy() or strncpy() functions

Aim: Copying contents of one string into another using strcpy()


Input: char str1[100],str2[100],str3[100]

Logic: strcpy(str2,str1), strncpy(str3,str2,n)

Output: Copied string using strcpy()


C program:

#include<stdio.h>

#include<string.h>

int main()

char str1[100],str2[100],str3[100];

int n;

scanf("%s",str1);

strcpy(str2,str1);
scanf("%d",&n);

strncpy(str3,str2,n);

printf("String copy using strcpy() : %s\n",str2);

printf("String copy using strncpy() : %s",str3);

return 0;

PROBLEM STATEMENT :
Write a C program that will demonstrate the use of calloc() or malloc() for
dynamic memory allocation and realloc() for resizing the allocated
memory.

Aim: Dynamic Memory Allocation and Reallocation

Input: int n,m,*ptr,i

Logic: ptr=(int*)calloc(n,sizeof(int)), ptr=(int*)realloc(ptr,(n+m)*sizeof(int))

Output: Dynamic Memory Allocated and Reallocated

C Program:
#include<stdio.h>

#include<stdlib.h>

int main(){

int n,m,*ptr,i;

scanf("%d",&n);

ptr=(int*)calloc(n,sizeof(int));

if(ptr==NULL)

printf("Memory not allocated");

return 1;

for(i=0;i<n;i++)

scanf("%d",(ptr+i));

scanf("%d",&m);

ptr=(int*)realloc(ptr,(n+m)*sizeof(int));

if(ptr==NULL)

printf("Memory not allocated");

return 1;
}

for(i=n;i<n+m;i++)

scanf("%d",(ptr+i));

for(i=0;i<n+m;i++)

printf("%d ",*(ptr+i));

free(ptr);

return 0;

PROBLEM STATEMENT :
Write a program to find the length of a given string without
using strlen() function.

Aim: Length of a string

Input: char str[100],int i=0

Logic: while(str[i]!='\0'),i++

Output: Length of a string

C Program:

#include<stdio.h>

#include<string.h>

int main()

char str[100];

int i=0;

scanf("%s",str);

while(str[i]!='\0'){

i++;

printf("Length of the string without using strlen() is %d",i);

return 0;

You might also like