I'm working on a program that's supposed to generate a 40 character random string of uppercase letters A-Z, then generate a random replacement string of random uppercase letters A-Z of length between 2 and 20. It displays the 40 character string, but when I enter the length of the replacement string, it says that Lab4B.exe stopped working and the program terminates. Anyone know what the problem could be?

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main (void)
{
	char s1[40];
	char s2[20];
	char c=' ';
	int index;
	int a=0;
	
		for(index=0; index<40; index++)
	{
		s1[index]=rand()%26+'A';
			
	}
		s1[40]='\0';
		printf ("%s", s1);
	printf("\n Enter length of replacement string:");
	scanf("%d",a);
	for(index=0; index<a; index++)
	{
		s2[index]=rand()%26+'A';
			
	}
	return 0;
	
}

Educated guess: scanf("%d",a) is missing the address-of operator on a . Often this results in a miserable crash, and it's very easy to forget that it should be &a .

Okay, I put the & before a, and now it's saying that the stack around variable s1 is corrupted when I enter it. any advice?

What value did you enter?

You created a buffer overflow, and corrupted the stack. Line 17: there's no such thing as s1[40]. The maximal index legal for s1 is 39.

when you declared s1[40], you created variables s1[0], s1[1], .... upto s1[39].....
s1[40] does not exist......

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
int main (void)
{
	char s1[40];
	char s2[20];
	char c=' ';
	int index;
	int a=0;
	
		for(index=0; index<39; index++)
	{
		s1[index]=rand()%26+'A';
			
	}
		s1[39]='\0';
		printf ("%s", s1);
	printf("\n Enter length of replacement string:");
	scanf("%d",&a);
	for(index=0; index<a; index++)
	{
		s2[index]=rand()%26+'A';
			
	}
	return 0;
	
}
commented: Isn't that what Naz just said? Why repeat the now obvious? -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.