Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 29th, 2006, 7:23 AM   #1
Uday
Programmer
 
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0 Uday is on a distinguished road
Problem with strings

This program is not terminating. Can anyone tell me what is the problem with the code.

#include<Stdio.h>
#include<String.h>
void main()
{
int i=0;
char str1[10];
clrscr();
printf("Enter string1");
scanf("%s",str1);
for(i=0;str1!='\0';i++)
{
printf("%d",i);
}
getch();
}

Is that all strings end with the null character '\0'
Uday is offline   Reply With Quote
Old Mar 29th, 2006, 7:44 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
I sure hope you are kidding...
What in the world are you trying to do?

#include<stdio.h>
#include<string.h>
#include <conio.h>

int main()
{
	int i;
	char str1[10];
	printf("Enter string1\n");
	scanf("%s",str1);
	printf("%s", str1);
	getch();
	return 0;
}

Yes C strings end with the null character. You don't have to tell us what it is, believe me, we know.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 29th, 2006, 7:54 AM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
for(i=0;str1!='\0';i++)
{
printf("%d",i);
}
Your loop condition is on str1!='\0', but you don't change the value of str1 inside the loop. I suspect you meant
for(i=0;str1[i]!='\0';i++)
The Dark is offline   Reply With Quote
Old Mar 29th, 2006, 9:33 PM   #4
Uday
Programmer
 
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0 Uday is on a distinguished road
I wrote this program to test whether the sting ends with '\0'. I noticed that there is some problem with this part (I was trying another program that is a bigger one. That program is posted below). I corrected the above one. I had to put str1[i]

The original program that is not working is

Q)
Write a program to transform its input according to a specified transformation scheme. The transformation scheme will consist of two strings: a string of characters and then a string of replacement characters. The idea is that your program replaces every instance of the i'th character in the initial string with the i'th character in the replacement string (see example output below). Your transformation scheme can include letters (uppercase and lowercase), numbers and punctuation. When no substitution is defined for a character, the program just passes it through to the output unchanged. Your program should check that each transformation scheme contains two strings and that the strings contain the same number of characters. The program should inform the user of any errors in the transformation scheme. Your program should also have a list of 10 phrases to which it will apply transformation schemes. For each of these phrases, your program writes the phrase before and after the substitutions have been made.

Example:

Input Line: abcde bcead

Output:
Transformation Scheme - abcde bcead

alex was here
bldx wbs hdrd

a better example
b cdttdr dxbmpld

#include<Stdio.h>
#include<String.h>
void main()
{
int i,j;
char phrase[10][20],charstr[10],repstr[10];
clrscr();
printf("Enter 10 phrases to be tranformed");
 for(i=0;i<10;i++)
  {
   scanf("%s",phrase[i]);
  }
printf("Enter the character string\n");
scanf("%s",charstr);
printf("Enter the replacement string\n");
scanf("%s",repstr);
i=0;
while(repstr[i]=='\0')
 {
  printf("The Repstr is null and the output is\n");
  for(i=0;i<10;i++)
   {
    printf("%s\n",phrase[i]);
   }
 }
i=strlen(repstr);
j=strlen(charstr);
printf("%d%d",i,j);
if(i==j)
 {
   for(i=0;i<10;i++)
   printf("%s",phrase[i]);
   for(j=0;phrase[i][j]!='\0';j++)
	{
	if(phrase[i][j]==charstr[j])
		{
		phrase[i][j]=repstr[j];
	 printf("%s",phrase[i]);
		}
	}
 }
else
 {
printf("The transformation cannot take place since charstr!=repstr");
 }
getch();
}
Uday is offline   Reply With Quote
Old Mar 29th, 2006, 10:23 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm going to suggest that you go through your loops and other machinations once with a pencil and paper, tracking the array index and pointer values of anything you use. Make sure you DO understand C-string char arrays and pointers. You'll pick up a lot that will stick in your mind, that way. I would suggest that you limit the number of input characters allowed to prevent buffer overflow and the attendant crashes and other headaches. You may do that with scanf, or you may switch to fgets. Consult your documentation.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 29th, 2006, 11:10 PM   #6
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Your welcome, by the way.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC