int truncate(char str[], unsigned int index);
Create a function called truncate() that takes a string and integer as a parameter. Your function should truncate the string by placing a NULL byte at the specified index. You should return the index passed as a parameter if successful, -1 on fail. For example, if the index given is 5, but the string is “fun”, you would return -1 because the string is not long enough.
#source code in c language:
#include <stdio.h>
int truncate(char str[], unsigned int index);
int main(){
printf("%d\n",truncate("fun",5));
return 0;
}
int truncate(char str[], unsigned int index){
for(int j=0;j<str[j]!='\0';j++){
if(j==index){
return index;
}
}
return -1;
}
#if you need any information comment below..i will respond as possible as soon.thanks..
Get Answers For Free
Most questions answered within 1 hours.