Practice Programs
- Doing Programs gives you the great understanding about what you have learned.
- Doing Programs gives you the Confidence.
- So let's Start by doing some programming.
-
Q:declare char type array and take elements from the user and prints its length?
Solution:
#include<stdio.h> void main() { int i; char s1[20]; printf("Enter the string:"); gets(s1); //just iterating characters of s1 string and incrementing i value //until null character is not get. for(i=0;s1[i] != '\0';i++){ //body of for loop. } printf("Length of string s1 is %d",i); }
Output:
Enter the string: Programmer
Length of string s1 is 10.Exercise:
Q:Declare two character array and take characters from the user and print the difference between their length of string?
-
Q:declare two character array and take both string from the user and compare them by using built-in function and print the result?
Solution:
#include<stdio.h> void main() { int result; char s1[20],s2[20]; printf("Enter the string s1:"); gets(s1); printf("Enter the string s2:"); gets(s2); printf("\n"); //tasking cursor to a new line. result = strcmp(s1,s2); // using strcmp() function on string s1 and s2 //and assigning the return value from strcmp() //to a variable result. if(result > 0) printf("s1 is greater than s2"); if(result < 0) printf("s1 is less than s2"); else printf("Both strings are equal."); }
Output:
Enter the string s1:Programmer
Enter the string s2:Programmer
Both strings are same.Exercise:
Q:declare two character array and take both string from the user and compare only 4 characters by using built-in function and print the result?
-
Q:Declare a character array and reverse the string without using the built-in function and print it on the screen?
Solution:
#include<stdio.h> void main() { int i,j,l; char s[20] ,ch; printf("Enter the string:"); gets(s); for(i=0;s[i]!='\0';i++) { //to calculate the length of string. //this will just return the value of i //which will be the length of the string. //body of for loop } for(j=0;j<i/2;j++) { //swapping the elements. //at every iteration //we are swapping s[i] element with s[(i-1)-1] element. //where i is length of string. ch = s[j]; s[j] = s[(i-1)-j]; s[(i-1)-j] = ch; } printf("reverse string is %s",s); }
Output:
Enter the string: Programmer
Reverse string is remmargorP.Exercise:
Q:declare a character array and take string from the user and check if it is palindrome string or not an palindrome string
Hint: Palindrome string is string which remains unchanged, when we reverse it. Example: Madam, Civic, Level, Refer, Stats
-
Q:Declare a character array 's' and character 'c' and take their value from the user and find character 'c' in the string 's' ,if character 'c' is present in the string 's' then print its position otherwise print the message character not found.
Solution:
#include<stdio.h> void main() { char s[20]; char c; int i,flag,index; //declaring variables printf("enter the string:"); gets(s); printf("\nEnter the character you want of find:"); c = getchar(); for(i=0; s[i]!= '\0'; i++) { if(s[i] == c) { //if character matches with array element //we are assigning the variable flag = 0,and //adding 1 to i and assigning it to index variable. //and breaking the loop. flag = 0; index = (i+1); break; } } //as we have assign flag=0,when character is matched, //so we will check flag value if it is 0 then will print //character found and it's position. //otherwise printing character not found. if(flag == 0) { printf("Character found at position %d",i+1); } else printf("Character Not found"); }
Output:
Enter the string:student
Enter the character you want of find:e
Character found at position 5.Exercise:
Q:Declare a character array 's' and character 'c' and take their value from the user and find character 'c' in the string 's' ,if character 'c' is present in string than print the position of character at last occurrences of the character.
- do more practice problems on string handling functions,
for understanding the concept more better.
Further Topics:
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- Practice programs
People Also Searched:
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() functions
- Strings In C
- Concept of strings
- Concept of Initializing One Dimensional Array
- How to take array elements as input from the User
- How to declare 1D Array?
- How to Initialize 1D Array?
- What are Array?
- Need Of Array
- Why Array?
- Concept of Array
0 Comments
Post a Comment