![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0
![]() |
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' |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
for(i=0;str1!='\0';i++)
{
printf("%d",i);
}for(i=0;str1[i]!='\0';i++) |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Oct 2005
Location: India
Posts: 30
Rep Power: 0
![]() |
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();
} |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 894
Rep Power: 4
![]() |
Your welcome, by the way.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|