Hi friends,
I have an assignment in C where I have to insert a user inputted character into a string at the position the user states. I have the code below but it does not seem to work, let alone compile. I hate to come here for homework help, but I believe that its best learning by being taught by smart people, such as the users here! Anyways, the code I have so far is below.
The output should be like this:Code:# include <stdio.h># include <string.h> void insert (char origString[], char insertion_char, int position_of_insertion) { int new_length = strlen(origString) + 1; char new_str[new_length]; } int main() { char origString[20], insertion_char[1], new_str; int position_of_insertion, i, new_length; printf("Enter a string: "); scanf("%s", origString); printf("Enter a character to insert: "); scanf("%s", insertion_char); printf("Which position should the character be inserted before: "); scanf("%d", &position_of_insertion); for (i=0; i<position_of_insertion; i++) { new_str[i]= insert(origString, insertion_char, position_of_insertion); } for (i=position_of_insertion+1; i<new_length; i++) { new_str[i] = insert(origString, insertion_char, position_of_insertion); } printf("The new string is: %s", new_str); return 0; }
Code:Enter a String:lovecats4ever Enter a character to insert: I Which position should the character be inserted before:0 The new string is Ilovecats4ever



1Likes
LinkBack URL
About LinkBacks


