Write a C Program to extract a portion of a string from a character string using for loop -
#include<stdio.h> #include<string.h> void main() { char str1[50],str2[50]; int i,j,n,m,l; clrscr(); printf("enter string\n"); gets(str1); //scanf ("%s",str1); printf("enter position of required character: "); scanf("%d",&n); printf("enter required number of characters extracted: "); scanf("%d",&m); l=strlen(str1); if(m+n-1<l) { for(i=n-1;i<m+n-1;i++) { for(j=0;j<i;j++) { str2[j]=str1[i]; str2[j]='\0'; } } printf ("the extracted string is: %s",str2); } else printf ("string extraction not possible"); }
expected output
enter string : university in bangalore enter position of required character: 6 enter required number of characters extracted: 4 extracted string is: sity
package exatractstring; import java.util.scanner; public class exatractstring { public static void main(string[] args) { string str; int begin,end; scanner s = new scanner(system.in); scanner sc = new scanner(system.in); system.out.println("enter string : "); str = s.nextline(); int sizestring = str.length(); system.out.println("enter extract begining position : "); begin = sc.nextint(); system.out.println("enter extract end position : "); end = sc.nextint(); if(begin < 0 || end>sizestring ) { system.out.println("you wrong ..!!!"); } system.out.println(str.substring(begin,end)); } }
Comments
Post a Comment