gpt4 book ai didi

c - 编写一个C程序,使用for循环从字符串中提取字符串的一部分

转载 作者:行者123 更新时间:2023-11-30 21:02:57 25 4
gpt4 key购买 nike

#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
int i,j,n,m,l;
clrscr();
printf("Enter the string\n");
gets(str1); //scanf ("%s",str1);
printf("Enter the position of the required character: ");
scanf("%d",&n);
printf("Enter the required number of characters to be 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 is not possible");
}

预期输出

Enter the string : UNIVERSITY IN BANGALORE

Enter the position of the required character: 6

Enter the required number of characters to be extracted: 4

The 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 a 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 are wrong ..!!!");
}
System.out.println(str.substring(begin,end));
}

}

关于c - 编写一个C程序,使用for循环从字符串中提取字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27322515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com