8 Replies - 2509 Views - Last Post: 04 September 2009 - 06:37 AM Rate Topic: -----

#1 yuuske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 09

inserting a character in a string and deleting a character from a stri

Posted 03 September 2009 - 05:20 AM

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
char *str;
int ind,pos,nw=0;
printf("\nEnter string: ");
gets(str);
printf("Enter position of character you want to delete(start with 0): ");
scanf("%d:,&pos);
for (ind=0;str[ind]!='\0';ind++)
				{
				if (ind!=pos)
					   {
					   str[nw]=str[ind];
					   nw++;
					   }
				}
str[nw-1]='\0';
printf("%s",str);
getch();
return 0;
}



This is my code for the program that deletes a character from a string.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
char *str,nwch;
int ind,pos,nw=0;
printf("\nEnter string: ");
gets(str);
printf("\nEnter character you want to insert: ");
nwch=getchar();
printf("\nEnter position where you want to insert new character(start with 0): ");
scanf("%d",&pos);
for (ind=0;str[ind]!='\0';ind++) //sorry got a typo here XD
		  {
		  if (ind!=pos)
				  {
				  str[nw]=str[ind];
				  nw++;
				  }
		  else if (ind==pos)
				 {
				  str[nw]=nwch;
				  nw++;
				 }
		   }
printf("\n%s",str);
getch();
return 0;
}



my codes for the character program insertion

.....my character deleting program works just fine...and the insertion program is just its opposite twin...problem is my insertion program doesnt work the way i want it to >.< if i typed in "dta" and i want to insert "a" at index "1" its supposed to output "data"
but it doesnt >.<

any advise from the masters?and yea this has to be done in turbo c using old fashion looping ^_^

This post has been edited by yuuske: 03 September 2009 - 05:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: inserting a character in a string and deleting a character from a stri

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: inserting a character in a string and deleting a character from a stri

Posted 03 September 2009 - 05:28 AM

should the test in your for()
for (ind=0;str[ind]='\0';ind++)


be !=
for (ind=0;str[ind]!='\0';ind++)


Was This Post Helpful? 0
  • +
  • -

#3 dgirdhar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 13-June 08

Re: inserting a character in a string and deleting a character from a stri

Posted 03 September 2009 - 05:43 AM

Still this will not solve the problem,

You are incrementing the varaibles 'ind' in case you found the position.

In first iteration
Str[ind/0] = 'd'

In Second iteration
Str[ind/1] = 'a' // using else condition.
(Error: Here you are overriding 't'.)
Here 't' is lost and string will be now ('daa')

Solution Suggestion
1) Take new empty string and store data their or
2) First store new character at end and shift according to position.


View Postyuuske, on 3 Sep, 2009 - 04:20 AM, said:

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
char *str;
int ind,pos,nw=0;
printf("\nEnter string: ");
gets(str);
printf("Enter position of character you want to delete(start with 0): ");
scanf("%d:,&pos);
for (ind=0;str[ind]!='\0';ind++)
				{
				if (ind!=pos)
					   {
					   str[nw]=str[ind];
					   nw++;
					   }
				}
str[nw-1]='\0';
printf("%s",str);
getch();
return 0;
}



This is my code for the program that deletes a character from a string.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
char *str,nwch;
int ind,pos,nw=0;
printf("\nEnter string: ");
gets(str);
printf("\nEnter character you want to insert: ");
nwch=getchar();
printf("\nEnter position where you want to insert new character(start with 0): ");
scanf("%d",&pos);
for (ind=0;str[ind]='\0';ind++)
		  {
		  if (ind!=pos)
				  {
				  str[nw]=str[ind];
				  nw++;
				  }
		  else if (ind==pos)
				 {
				  str[nw]=nwch;
				  nw++;
				 }
		   }
printf("\n%s",str);
getch();
return 0;
}



my codes for the character program insertion

.....my character deleting program works just fine...and the insertion program is just its opposite twin...problem is my insertion program doesnt work the way i want it to >.< if i typed in "dta" and i want to insert "a" at index "1" its supposed to output "data"
but it doesnt >.<

any advise from the masters?and yea this has to be done in turbo c using old fashion looping ^_^

Was This Post Helpful? 0
  • +
  • -

#4 yuuske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 09

Re: inserting a character in a string and deleting a character from a stri

Posted 03 September 2009 - 05:52 AM

TPAM you got me lost with the first suggestion >.<

how do i make a new one when an arrays memory tends to be a fixed value compared to that of a singly linked list?

sorry im really slow >.< but im trying hard
Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: inserting a character in a string and deleting a character from a stri

Posted 03 September 2009 - 06:20 AM

View Postyuuske, on 3 Sep, 2009 - 11:52 AM, said:

TPAM you got me lost with the first suggestion >.<

how do i make a new one when an arrays memory tends to be a fixed value compared to that of a singly linked list?

sorry im really slow >.< but im trying hard

you could move the characters in the string to make space then insert the new character
for (ind=0;str[ind]!='\0';ind++) //sorry got a typo here XD
		  {
		   if (ind==pos)
				 {
				  int i;
				// make room in string for character
				  for(i=strlen(str)+2;i>=pos;i--) str[i+1]=str[i];
				  str[pos]=nwch;
				 }
		   }


in fact you don't need the outer loop you can insert directy
if(strlen(str)>=pos)
	{
	   int i;
	   // make room in string for character
	   for(i=strlen(str)+2;i>=pos;i--) str[i+1]=str[i];
	   str[pos]=nwch;
	 }


Was This Post Helpful? 0
  • +
  • -

#6 yuuske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 09

Re: inserting a character in a string and deleting a character from a stri

Posted 03 September 2009 - 06:24 AM

ack! ....

i failed to amuse myself ahaha

tnx i'll take that into account

tnx alot
Was This Post Helpful? 0
  • +
  • -

#7 yuuske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 09

Re: inserting a character in a string and deleting a character from a stri

Posted 04 September 2009 - 05:59 AM

hi im back again...i tried the solution that mr horace gave me but i get an output of "daaa"

Enter string: dta
Enter character to insert: a
Enter position where to: 1

Output:
daaa

>.<
Was This Post Helpful? 0
  • +
  • -

#8 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: inserting a character in a string and deleting a character from a stri

Posted 04 September 2009 - 06:11 AM

View Postyuuske, on 4 Sep, 2009 - 11:59 AM, said:

hi im back again...i tried the solution that mr horace gave me but i get an output of "daaa"

Enter string: dta
Enter character to insert: a
Enter position where to: 1

Output:
daaa

>.<

strange, using my example
#include<stdio.h>
#include<conio.h>
main()
{
//clrscr();
char *str,nwch;
int ind,pos,nw=0;
printf("\nEnter string: ");
gets(str);
printf("\nEnter character you want to insert: ");
nwch=getchar();
printf("\nEnter position where you want to insert new character(start with 0): ");
scanf("%d",&pos);
if(strlen(str)>=pos)
	{
	   int i;
	   // make room in string for character
	   for(i=strlen(str)+2;i>=pos;i--) str[i+1]=str[i];
	   str[pos]=nwch;
	 }

printf("\n%s",str);
getch();
return 0;
}


i run it it works
Enter string: dta
Enter character you want to insert: a
Enter position where you want to insert new character(start with 0): 1
data


Was This Post Helpful? 0
  • +
  • -

#9 yuuske  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 09

Re: inserting a character in a string and deleting a character from a stri

Posted 04 September 2009 - 06:37 AM

i get it now...my brackets placing was wrong demn...sry for the trouble and tnx again mr horace ^_^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1