Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Java Introduction

Strings in C are arrays of characters terminated by a null character. Strings can be declared like arrays using char arrays or pointers. Common functions for working with strings include scanf to read input, gets and puts to read and output strings, and loops to read a line of characters. The string.h header includes functions for calculating the length of a string and other string operations.

Uploaded by

arunkumar_a8131
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Java Introduction

Strings in C are arrays of characters terminated by a null character. Strings can be declared like arrays using char arrays or pointers. Common functions for working with strings include scanf to read input, gets and puts to read and output strings, and loops to read a line of characters. The string.h header includes functions for calculating the length of a string and other string operations.

Uploaded by

arunkumar_a8131
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to Strings Strings in C

“ARUN” ,’a’;
Strings in c are nothing but an
array of characters terminated
by a null character
Strings are decalred in the
same manner as that of arrays
Declare an string
Char str[6]=”Hello”; last
character in a string will be
null charater that shows the
end of the string
Declaration of string
Char *name;
Initialization of strings
Char str[]=”Hello”;
Char str[50]=”Hello”;
Chat str[]={‘H’,’e’,’l’,’l’,’o’,’\0’};
Char *name=”Hello”;
Reading from the user
%s

Example 1 #include <stdio.h>


int main()
{
char name[20];
printf("Enter the name\n");
scanf("%s",name);
printf("Your name is %s\n",name);
return 0;
}

Example 2 When you use scanf to read a string any #include <stdio.h>
white space character will be considered as int main()
termination it will stop further reading in case of {
scanf char name[100];
Now how to read a line of text using scanf function printf("Enter the name\n");
scanf("%[^\n]",name);
printf("%s\n",name);
return 0;
}

Example 3 Reading a string using gets and puts #include <stdio.h>


function
int main()
{
char name[30];
printf("Enter your name\n");
gets(name);
printf("Name : ");
puts(name);
return 0;
}

Example 4 Reading a line of text using loops #include <stdio.h>

int main()
{
char str[100],ch;
int i=0;
printf("Input a string\n");
while(ch!='\n'){
ch=getchar();
str[i]=ch;
i++;
}
str[i]='\0';
printf("String read is : %s",str);
return 0;
}

Example 5 There is header file called string.h which #include <stdio.h>


has inbuilt functions required for working with
strings int main()
Without using the library functions {
Find the length of the string char str[100];
int i;
printf("Enter a string\n");
scanf("%[^\n]",str);
for(i=0;str[i]!='\0';i++);
printf("Length of the string is %d\n",i);
return 0;
}

You might also like