2017-12-23 15 views
-3

안녕하세요,잘라 내기에 대한 도움이 필요합니다.

간단한 프로그램에 대한 아이디어가 필요합니다.

문자열을 n 문자로 줄이는 함수를 작성하십시오. 문자열이 이미 n보다 짧으면 함수는 문자열을 변경하지 않아야합니다. 가정 프로토 타입은

void truncate(char *str, int inLen); 

그냥 간단한 설명을 제공입니다 ..

감사합니다 당신은 그래서 지금 내가 여기에 C에서 문제 시도를 줄 수있는 자신의 somelogic을 시도하기 때문에

+1

는 왜 우리가 당신의 일을 기대합니까? –

+2

스택 오버플로에 오신 것을 환영합니다. 이것은 숙제 완료 서비스가 아닙니다. 강사가 우리가 아니라 과제를 주었고, 당신은 자신의 일을해야 할 것입니다. 시작할 수 없다면 교사에게 도움을 요청하십시오. 그들은 당신을 가르치기 위해 돈을 받고 있습니다. [help/on-topic]은 숙제 도움을 요청하는 질문 **에는 문제를 해결하기 위해 지금까지 해 온 작업의 요약과 문제 해결에 대한 설명이 포함되어야합니다. *** 행운을 빕니다. –

+0

어떤 언어로이 작업을 수행하려고합니까? – jhpratt

답변

0

여기

void truncate(char *str, int inLen){ 
    int len=strlen(str); 
    char *newstr; 
    newstr=(char *)malloc(inLen*sizeof(char)); 
    if(inLen>len) 
     strcpy(newstr,str); 
    else{ 
     strncpy(newstr,str,inLen); 
    } 
    printf("%s",newstr); 
} 

C++ 솔루션 :

여기
#include<iostream> 
#include<string.h> 
using namespace std; 

void truncate(char *str, int inLen){ 
    int len=strlen(str); 
    char *newstr=new char[inLen]; 
    if(inLen>len) 
     strcpy(newstr,str); 
    else{ 
     strncpy(newstr,str,inLen); 
    } 
    cout<<newstr; 
} 
int main() 
{ 
char str[100]; 
int inLen; 
cin>>inLen; 
cin>>str; 
truncate(str,inLen); 
return 0; 
} 

파이썬 :

string=input("Enter string") 
inLen=int(input("Enter trim length")) 
newstring=string[0:inLen] 
print(newstring) 
+0

감사합니다 Shahil Jain – malgmo956

+0

당신이 그것을 찾은 것을 부탁드립니다. :) –

+0

나는 upvote 수 없습니다. 그러나 도움이되는 똑딱 거리십시오. 더 많은 명성을 필요로 upvote하려면 .. – malgmo956

0
#include<iostream> 
using namespace std; 
#include<string.h> 

void truncateCharArray(int maxLength , char * inLen) 
{ 
if ((0 == inLen) || !(0 < maxLength)) 
return; 
cout<<"To my eye, this is confusing. Why not simply"<<endl; 

if (inLen == NULL || maxLength <= 0) 
return; 
if (maxLength < strlen(inLen)) 
cout<<"What if maxLength is equal to the length of buffer?"<<endl; 
{ 
inLen[maxLength] = '\0'; 
} 
return; 
} 

int main() 
{ 
    truncateCharArray(30,'dsd'); 
    return 0; 
} 


That what I have already tried.