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

Assignment 1

The document discusses string functions in C and provides examples of each. It lists functions like strlen(), strcpy(), strcat(), strcmp(), strrev(), strupr() and explains what each does. Examples are given to demonstrate how to use each string function, outputting the length of a string, copying one string to another, concatenating strings, comparing strings, reversing strings, and converting a string to uppercase. The document also explains the concepts of one-dimensional and two-dimensional arrays with examples.

Uploaded by

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

Assignment 1

The document discusses string functions in C and provides examples of each. It lists functions like strlen(), strcpy(), strcat(), strcmp(), strrev(), strupr() and explains what each does. Examples are given to demonstrate how to use each string function, outputting the length of a string, copying one string to another, concatenating strings, comparing strings, reversing strings, and converting a string to uppercase. The document also explains the concepts of one-dimensional and two-dimensional arrays with examples.

Uploaded by

Dr. SATHIYA M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 1

1. a) List out various string functions in C and explain each function with example
2. Explain the concept of one dimensional and two dimensional arrays with example
Assignment 1
List out various string functions in C and explain each function with example

C String Functions
There are many important string functions defined in "string.h" library.

No Function Description
.

1) strlen(string_name) returns the length of string name.

2) strcpy(destination, copies the contents of source string to destination


source) string.

3) strcat(first_string, concats or joins first string with second string. The


second_string) result of the string is stored in first string.

4) strcmp(first_string, compares the first string with second string. If both


second_string) strings are same, it returns 0.

5) strrev(string) returns reverse string.

6) strlwr(string) returns string characters in lowercase.

7) strupr(string) returns string characters in uppercase.

C String Length: strlen() function


The strlen() function returns the length of the given string. It doesn't count null
character '\0'.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4. char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};    
5.    printf("Length of string is: %d",strlen(ch));    
6.  return 0;    
7. }    

Output:

Length of string is: 10


C Copy String: strcpy()
The strcpy(destination, source) function copies the source string in destination.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.  char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};    
5.    char ch2[20];    
6.    strcpy(ch2,ch);    
7.    printf("Value of second string is: %s",ch2);    
8.  return 0;    
9. }    

Output:

Value of second string is: javatpoint

C String Concatenation: strcat()


The strcat(first_string, second_string) function concatenates two strings and result is
returned to first_string.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.   char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};    
5.    char ch2[10]={'c', '\0'};    
6.    strcat(ch,ch2);    
7.    printf("Value of first string is: %s",ch);    
8.  return 0;    
9. }    

Output:

Value of first string is: helloc

C Compare String: strcmp()


The strcmp(first_string, second_string) function compares two string and returns 0 if
both strings are equal.

Here, we are using gets() function which reads string from the console.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.   char str1[20],str2[20];    
5.   printf("Enter 1st string: ");    
6.   gets(str1);//reads string from console    
7.   printf("Enter 2nd string: ");    
8.   gets(str2);    
9.   if(strcmp(str1,str2)==0)    
10.       printf("Strings are equal");    
11.   else    
12.       printf("Strings are not equal");    
13.  return 0;    
14. }    

Output:

Enter 1st string: hello


Enter 2nd string: hello
Strings are equal
Next TopicC strrev()

C Reverse String: strrev()


The strrev(string) function returns reverse of the given string. Let's see a simple
example of strrev() function.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.   char str[20];    
5.   printf("Enter string: ");    
6.   gets(str);//reads string from console    
7.   printf("String is: %s",str);    
8.   printf("\nReverse String is: %s",strrev(str));    
9.  return 0;    
10. }    
Output:

Enter string: javatpoint


String is: javatpoint
Reverse String is: tnioptavaj

C Reverse String: strrev()


The strrev(string) function returns reverse of the given string. Let's see a simple
example of strrev() function.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.   char str[20];    
5.   printf("Enter string: ");    
6.   gets(str);//reads string from console    
7.   printf("String is: %s",str);    
8.   printf("\nReverse String is: %s",strrev(str));    
9.  return 0;    
10. }    

Output:

Enter string: javatpoint


String is: javatpoint
Reverse String is: tnioptavaj

C String Uppercase: strupr()


The strupr(string) function returns string characters in uppercase. Let's see a simple
example of strupr() function.

1. #include<stdio.h>  
2. #include <string.h>    
3. int main(){    
4.   char str[20];    
5.   printf("Enter string: ");    
6.   gets(str);//reads string from console    
7.   printf("String is: %s",str);    
8.   printf("\nUpper String is: %s",strupr(str));    
9.  return 0;    
10. }    

Output:
Enter string: javatpoint
String is: javatpoint
Upper String is: JAVATPOINT

You might also like