Uploaded by Abdullah Tahir

lab report 11

advertisement
LAB REPORT 11
WORKING WITH STRINGS IN C
|
COMSATS UNIVERSITY
ISLAMABAD
SUBMITTED TO
MAM. SADAF IQBAL
SUBMITTED BY
ABDULLAH TAHIR
DATE
13TH JUNE 2022
SUBJECT
INTRO.TO COMPUTER PROGRAMMING
CSC141
REGISTRATION #
FA21-BEE-211
DEPARTMENT OF ELECTRICAL
ENGINEERING
Answer:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=0,j=0;
char m2[30],m3[100];
char m1[40]="Programming is great fun!";//Declares a C-String called ‘m1’ and
initializes it with text “Programming is great fun!”
puts(m1); //Uses C-function puts() to print this string.
printf("Enter a string name : \n");
gets(m2);//Asks the user to enter a String named ‘m2’ (Hint: Use gets()
function for this.)
//Concatenates the two strings and stores the result in ‘m3’.
while(m1[i]!='\0')
{
m3[j]=m1[i];
i++;
j++;
}
i=0;
while(m2[i]!='\0')
{
m3[j]=m2[i];
i++;
j++;
}
m3[j]='\0';
puts(m3);
return 0;
}
Output:
Answer:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
int strconverter(char* string)
{
int num = 0;
for (int i = 0; string[i] != '\0'; i++)
num = num * 10 + string[i] - '0';
return num;
}
int main(){
char *str = "124";
int number = strconverter(str);
printf("String to integer = %d",number);
}
Output:
Answer:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void main()
{
char space[]="Grim return to the planet of apes!!";
int i;
for(i=0;space[i]!=0;i++)
{
if(space[i]==32&&space[i+1]==32)
{
for(;space[i+1]!=0;i++)
{
space[i]=space[i+1];
}
space[i]='\0';
i=0;
}
}
printf("\n%s",space);
}
Output:
CRITICAL ANALYSIS:
In this lab we have learned the basic operations with Strings, differentiate
between C Strings and 2D arrays, using built-in C functions for string
manipulation and develop custom functions for C String manipulation.
Download